Question

So most of the question is in the title.

I've already set up everything in GCMIntentService to do a sendbroadcast(intent) in one function. But when I try to do it in another one I get this error "Sending message to a Handler on a dead thread". Code is exactly the same, works perfect for one not for the other any ideas ?

@Override
protected void onMessage(Context context, Intent intent) {
  Log.i(TAG, "Received message");
  final String message = intent.getExtras().getString("clicked");
  if(message == null)
    Toast.makeText(context, getResources().getString(R.string.message_null),      Toast.LENGTH_SHORT).show();
  else if(message.split("\\|")[0].equals("updateMessageInvisible")) {
    onReceiveUpdateMessageInvisible(message);
  } else if(message.split("\\|")[0].equals("message")) {
    onReceiveMessage(message);
  }
}

This one works fine.

private void onReceiveMessage(String message) {
  //Stuff happening (accessing database mostly)

  //Notify listView of Message that it has new data
  Intent i = new Intent("com.crisis.app.NEW_MESSAGE");
  sendBroadcast(i);
}

This one doesn't work.

private void onReceiveUpdateMessageInvisible(String message) {
  //Stuff happening (accessing database again)

  //Notify listView of Message that data has been updated
  Intent i = new Intent("com.crisis.app.UPDATE_MESSAGE");
  sendBroadcast(i);
}

My broadcast receiver class :

public final class ReceiveMessage extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) 
  {
    System.out.println("NOTIFY DATA CHANGED");
    //Stuff happening
    //Set adapter with array of message
    adapter = new MessageRowAdapter(context, values);
    listview.setAdapter(adapter);
  }
}

Of course I register and unregister in onPause and onResume.

So if anyone has an idea why it works in one scenario and not the other when there is no difference...

Maybe is it something about that you can only do one senBroadcast in an Intent ?

Thanks in advance !

Edited : Added function onMessage of GCMIntentService

Était-ce utile?

La solution

Fixed my issue.

It was coming from the fact that I was calling a Toast outside of the UIThread. Asynctask didn't solve it, you have to use :

new Thread() {
  public void run() {
    mainActivity.runOnUiThread(new Runnable() {
      public void run() {
        Toast.makeText(context, getResources().getString(R.string.message_null), Toast.LENGTH_SHORT).show();
      }
    });
  }
}.start();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top