8

I am using C2DM services and when I get message, I get also error of "Sending message to a handler on a dead thread" while displaying Toast message, where I want to see message, which arrived. Using code:

@Override
protected void onMessage(Context context, Intent intent) {
    Log.e("C2DM", "Message: arived");
    Bundle extras = intent.getExtras();
    if (extras != null) {
        //Toast.makeText(this.getApplicationContext(), (CharSequence) extras.get("payload"), Toast.LENGTH_LONG).show();

    }
}

onMessage method is used in class which extends C2DMBaseReceiver. Toast messatge never displays.

What is the error in here? Is there any soultion?

Edit:

09-06 08:59:02.135: WARN/MessageQueue(5654): Handler{44e65658} sending message to a Handler on a dead thread
09-06 08:59:02.135: WARN/MessageQueue(5654): java.lang.RuntimeException: Handler{44e65658} sending message to a Handler on a dead thread
09-06 08:59:02.135: WARN/MessageQueue(5654):     at android.os.MessageQueue.enqueueMessage(MessageQueue.java:179)
09-06 08:59:02.135: WARN/MessageQueue(5654):     at android.os.Handler.sendMessageAtTime(Handler.java:457)
09-06 08:59:02.135: WARN/MessageQueue(5654):     at android.os.Handler.sendMessageDelayed(Handler.java:430)
09-06 08:59:02.135: WARN/MessageQueue(5654):     at android.os.Handler.post(Handler.java:248)
09-06 08:59:02.135: WARN/MessageQueue(5654):     at android.widget.Toast$TN.hide(Toast.java:344)
09-06 08:59:02.135: WARN/MessageQueue(5654):     at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:55)
09-06 08:59:02.135: WARN/MessageQueue(5654):     at android.os.Binder.execTransact(Binder.java:288)
09-06 08:59:02.135: WARN/MessageQueue(5654):     at dalvik.system.NativeStart.run(Native Method)
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
Waypoint
  • 17,283
  • 39
  • 116
  • 170
  • I'm having the same problem, did you ever figure out a solution? – ToddH Sep 28 '11 at 00:37
  • I have moved from Toast message to Notification bar do display message i Got from server. I wasn't able to run it as a Toast. Goal is to carefully use context in your app and in a construstors – Waypoint Sep 28 '11 at 16:28

2 Answers2

5

There is a workaround. However I can't get it to work with that workaround.

I solved a similar problem by creating a Handler in the C2DMBaseReceiver constructor and added a Runnable that shows the Toast.

Something like this:

public void showToast(String message, Context context){
    handler.post(new DisplayToast(message, context));
}

private class DisplayToast implements Runnable{
    String mText;
    Context mContext;

    public DisplayToast(String text, Context context){
        mText = text;
        mContext = context;
    }

    public void run(){
        Toast.makeText(mContext, mText, Toast.LENGTH_LONG).show();
    }
}

And then you can just call the DisplayToast method from the subclass.

Hope it works!

William Wu
  • 69
  • 7
johan
  • 6,578
  • 6
  • 46
  • 68
  • Solved me a similar problem in adding views through WindowManager triggered by an incoming EventBus message. – Fanglin Jan 08 '17 at 05:36
3

See http://code.google.com/p/android/issues/detail?id=20915, which is a possible root cause of the problem. It includes a workaround for the issue.

Jonathan Perlow
  • 951
  • 1
  • 8
  • 12