Java.lang.RuntimeException: handler (android.os.Handler) sends a message to the dead thread handler

in my application I use IntentService to send SMS.

@Override protected void onHandleIntent(Intent intent) { Bundle data = intent.getExtras(); String[] recipients = null; String message = getString(R.string.unknown_event); String name = getString(R.string.app_name); if (data != null && data.containsKey(Constants.Services.RECIPIENTS)) { recipients = data.getStringArray(Constants.Services.RECIPIENTS); name = data.getString(Constants.Services.NAME); message = data.getString(Constants.Services.MESSAGE); for (int i = 0; i < recipients.length; i++) { if(!StringUtils.isNullOrEmpty(recipients[i])) { try { Intent sendIntent = new Intent(this, SMSReceiver.class); sendIntent.setAction(Constants.SMS.SEND_ACTION); PendingIntent sendPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent deliveryIntent = new Intent(this, SMSReceiver.class); deliveryIntent.setAction(Constants.SMS.DELIVERED_ACTION); PendingIntent deliveryPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, deliveryIntent, PendingIntent.FLAG_UPDATE_CURRENT); SmsManager.getDefault().sendTextMessage(recipients[i].trim(), null, "[" + name + "] " + message, sendPendingIntent, deliveryPendingIntent); } catch (Exception e) { Log.e(TAG, "sendTextMessage", e); e.printStackTrace(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); MainActivity.instance.writeToLogFile(e.getMessage(), System.currentTimeMillis()); } } } } } 

when I start the application, I get the following error:

 W/MessageQueue(7180): Handler (android.os.Handler) {42586468} sending message to a Handler on a dead thread W/MessageQueue(7180): java.lang.RuntimeException: Handler (android.os.Handler) {42586468} sending message to a Handler on a dead thread W/MessageQueue(7180): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294) W/MessageQueue(7180): at android.os.Handler.enqueueMessage(Handler.java:618) W/MessageQueue(7180): at android.os.Handler.sendMessageAtTime(Handler.java:587) W/MessageQueue(7180): at android.os.Handler.sendMessageDelayed(Handler.java:558) W/MessageQueue(7180): at android.os.Handler.post(Handler.java:323) W/MessageQueue(7180): at android.widget.Toast$TN.hide(Toast.java:367) W/MessageQueue(7180): at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:55) W/MessageQueue(7180): at android.os.Binder.execTransact(Binder.java:351) W/MessageQueue(7180): at dalvik.system.NativeStart.run(Native Method) 

My SMSReceiver is in a different class. How can I solve these problems? Thanks; Eyal.

+44
android handler android-intent service runtimeexception
Nov 18 '13 at 22:22
source share
2 answers

The problem is that you are creating a Toast inside a thread that is controlled by an IntentService . The system will use the Handler associated with this thread to show and hide Toast .

At first, Toast will be displayed correctly, but when the system tries to hide it, after the onHandleIntent method onHandleIntent finished, the error "send message to the handler on a dead thread" will be selected because the thread on which Toast was created is already invalid, and Toast will not disappear.

To avoid this, you should display a Toast message about the message in the main thread. Here is 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(); } }); 
+131
Nov 19 '13 at 3:58
source share

Map Toast to IntentService. try this code ..

 @Override public void onCreate() { super.onCreate(); mHandler = new Handler(); } @Override protected void onHandleIntent(Intent intent) { mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(MyIntentService.this, "Test", Toast.LENGTH_LONG).show(); } }); } 

source: - stack overflow

+2
Jun 16 '16 at 5:31
source share



All Articles