java.lang.RuntimeException: Handler (android.os.Handler) sending message to a Handler on a dead thread
The problem here is that you are creating a Toast
inside a thread that is managed by the IntentService
. The system will use the Handler
associated with this thread to show and hide the Toast
.
First the Toast
will be shown correctly, but when the system tries to hide it, after the onHandleIntent
method has finished, the error "sending message to a Handler on a dead thread" will be thrown because the thread on which the Toast
was created is no longer valid, and the Toast
will not disappear.
To avoid this you should show the Toast
posting a message to the Main Thread. Here's an example:
// create a handler to post messages to the main thread Handler mHandler = new Handler(getMainLooper()); mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show(); } });
Display Toast on IntentService.try this code..
@Overridepublic void onCreate() { super.onCreate(); mHandler = new Handler();}@Overrideprotected void onHandleIntent(Intent intent) { mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(MyIntentService.this, "Test", Toast.LENGTH_LONG).show(); } });}
I think you need to check a condition:
mHandler.getLooper().getThread().isAlive()
The application will just warn you about this error. Of course it s mostly not fatal one.But if there are many spawned usages of this handler these warnings will slow down the application.