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)
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!
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.
精彩评论