1

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?

PoeHaH
  • 1,936
  • 3
  • 28
  • 52

3 Answers3

1

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().

Swayam
  • 16,294
  • 14
  • 64
  • 102
1

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.

Updating progress dialog in Activity from AsyncTask

Community
  • 1
  • 1
Michael
  • 3,334
  • 20
  • 27
  • 1
    If you like my answer please remember to choose it as an accepted answer! Not only will it help me out, but people will see that you accept a lot of questions, and be more willing to help you out. – Michael Aug 30 '12 at 18:21
  • 1
    first I try things out before selecting the best answer ;) – PoeHaH Aug 30 '12 at 18:26
0

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().

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91