views:

664

answers:

3

I have tasks completed by AsyncTask in background. At some point I need to issue a Toast that something is completed.

I've tried and I failed because Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

How can I do that?

+4  A: 

onPostExecute - executes on UI thread or publishProgress(); in your doinbackground and

protected void onProgressUpdate(Integer... progress) {
}

http://developer.android.com/reference/android/os/AsyncTask.html

Alex Volovoy
I have to issue the Toast in the middle of the process, not in the end. What are my options?
Pentium10
onProgressUpdate. It also runs on UI thread and Toast should be fine
Alex Volovoy
+1  A: 

You can also use runOnUiThread method to manipulate your UI from background threads.

Aleksander O
Why the down vote? I think he has to call runOnUiThread.
Brandon
A: 

If you want to display the Toast from the background thread you'll have to call runOnUiThread from doInBackground. I don't believe there's another way.

Edit: I take that back. I think you can implement onProgressUpdate, which runs on the UI thread, to show the Toast and make calls to publishProgress from doInBackground.

Brandon