Can an async task send periodically a result to the GUI?
For example: I have 3 things I'd like my async task to do. After each thing, I'd like
to send a string back to the gui: "task x completed"
Can I do this without creating 3 async tasks?
Can an async task send periodically a result to the GUI?
For example: I have 3 things I'd like my async task to do. After each thing, I'd like
to send a string back to the gui: "task x completed"
Can I do this without creating 3 async tasks?
use onProgressUpdate()
to generate display in the UI .
boolean first = false, second = false, third = false; // task not completed yet
doInBackground()
{
// complete your task and set the corresponding boolean to **true**
}
onProgressUpdate(..)
{
if(first== true) Toast.makeText(context, "Task 1 finished", Toast.LENGTH_LONG).show();
//similarly check which task has been done
}
EDIT : You can also check out another method called publishProgress()
.
Yes. Check out the method onProgressUpdate() and publishProgress().
Here's an example i found very quickly, which may or may not work. But I think you have enough information to figure it out for yourself.
In addition to other suggestions: if you have access to activity from your asycntask, then you may also use Activity.runOnUiThread(Runnable action)
. Yet another option - if you have access to any view added on the screen, then you can also use it for safe UI updates via View.post(Runnable action)
. These are safe to call from doInBackground()
.