Reputation: 10049
I am learning Android via a book and would just like to confirm something.
When using AsyncTask according to the book it goes something like this:
main class
{
new AddStringTask().execute();
}
class AddStringTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... unused) {
// Do something
SystemClock.sleep(333);
return(something);
}
@Override
protected void onProgressUpdate(String... item) {
// update something
}
}
which creates one background thread to do some stuff. So if I want more threads, for example firing at different times (300, 500, 1000 milliseconds) then I need to make even more sub classes... true?
Or is there some way to do multiple threads firing at different times using just this one subclass?
Thanks!
Upvotes: 1
Views: 530
Reputation: 72321
You could also use the same AsyncTask and publish diffrent progresses from the same thread. Let say :
protected Void doInBackground(Void... unused) {
...
System.sleep(500);
publishProgress(x);
System.sleep(500);
pulbishProgress(y);
}
and
protected void onProgressUpdate(String... progress) {
myLabel.setText(progress[0]);
}
Upvotes: 1
Reputation: 26925
then I need to make even more sub classes... true?
Not true.
You can just execute the same AsyncTask
again by creating a new AddStringTask()
instance. This works since it'll be a new instance which differs from the other ones and each instance has its own thread. They are not interdependent.
However, the timer mechanism is something you have to implement yourself.
Upvotes: 3