I'm using Google Cloud Message to send a message to my phone from a server. When I include Toast.makeText()
inside of onHandleIntent
, it gave me an handler error:
java.lang.RuntimeException: Handler (android.os.Handler)
sending message to a Handler on a dead thread
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();
}
});
}
But when I tried having showToast()
function defined on the bottom of onHandleIntent
function in onHandleIntent()
, toast showed up fine. I know this is a handler problem from reading this java.lang.RuntimeException: Handler (android.os.Handler) sending message to a Handler on a dead thread
but I don't fully understand that answer. Could anyone explain more in detail so I can understand?
Thank you for your help in advance!