我使用Google Cloud Message从服务器向手机发送消息。当我在Toast.makeText()
中加入onHandleIntent
时,它给了我一个处理程序错误:
java.lang.RuntimeException: Handler (android.os.Handler)
在死线程上向处理程序发送消息
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
mes = extras.getString("text");
//**********this works***********
//showToast();
Log.i("GCM",
"Received : (" + messageType + ") "
+ extras.getString("iCal"));
//**********doesn't work***********
Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_LONG).show();
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showToast() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_LONG)
.show();
}
});
}
但是,当我尝试在showToast()
的{{1}}函数底部定义onHandleIntent
函数时,吐司显示正常。我知道这是阅读此java.lang.RuntimeException: Handler (android.os.Handler) sending message to a Handler on a dead thread
但我不完全理解这个答案。任何人都可以详细解释,以便我能理解吗?
提前感谢您的帮助!