I have set up an AsyncTask
.
In doInBackground()
, the only job is to call a method from another class. That method was copied from an old Java Applet, so while executing, it would print out progress every 10% onto the screen directly until finish. As this is not advisable in Android, I use AsyncTask
to run it.
My question is, how to capture the Integer progressPercent spit out from that method in doInBackground()
, and pass onto onProgressUpdate()
to update the UI? How should I modify the original method to cope with the AsyncTask
structure?
I think you need a Handler for that, because doInBackground()
will be called only once, so you cannot display the progress with that. You can update a progress bar for example with the handler in every 10% or even more often.
First the AsyncTask
class should be on the form
class Task extends AsyncTask<Object, Long, Object>
// or
class Task extends AsyncTask<Object, Integer, Object>
then edit the part that prints out progress every 10% inside the Task to
publishProgress(/*your progress here*/);
and finally your onProgressUpdate
and its parameter depends on the object that you choose in the class definition
@Override
protected void onProgressUpdate(Long... values) {
long progress = values[0];
// update UI here
}
// if you choose object Integer
@Override
protected void onProgressUpdate(Integer... values) {
int progress = values[0];
// update UI here
}
BTW: You can use a loop to show the progress many times instead of just once.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.