0

I am working on using the LocalBroadcastReceiver to send messages from an IntentService to an activity. I have a basic activity all the activities in my project inherit from that contains the activity code below. And a basic IntentService that is initialized by a WakefulBroadcastReceiver that contains the service code below.

In my activity I have:

@Override
protected void onCreate(Bundle savedInstanceState) {

    LocalBroadcastManager.getInstance(this).registerReceiver(
            mMessageReceiver, new IntentFilter("push-message"));
    super.onCreate(savedInstanceState);
}
 @Override
protected void onPause(){
    LocalBroadcastManager.getInstance(this).unregisterReceiver(
            mMessageReceiver);
    super.onPause();
}
@Override
protected void onResume(){
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(
            mMessageReceiver, new IntentFilter("push-message"));
}
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("message");
        Log.d("receiver", "Got message: " + message);
    }
};

And in my service I have:

public class SimpleMessagerService extends IntentService {
public SimpleMessagerService() {
    super("SimpleMessagerService");
}
@Override
protected void onHandleIntent(Intent intent) {
    Intent newintent = new Intent("push-message");
    // You can also include some extra data.
    String message = intent.getExtras().getString("message");
    newintent.putExtra("message", message);


    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}

The service onHandleIntent is being triggered, and when I put a break point in it and evaluate the expression: LocalBroadcastManager.getInstance(this); I can see the mMessageReceiver in the mReceivers list; however when I put a breakpoint in mMessageReceiver's onReceive, I find that it is never being triggered.

More info: It seems my service cannot actively do anything when it is called, but does not throw an exception. I tried saving my current context in the application file and throwing up a toast message from the service. The process seems to succeed, but the toast message never appears. This is what I have in the manifest for the service:

<service
        android:name="packagename.services.SimpleMessagerService"
        android:exported="false">
</service>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ryan McCaffrey
  • 201
  • 2
  • 15
  • 1
    Did you register the receiver in the manifest ? – Doug Ray Nov 09 '15 at 21:40
  • 1
    You don't need to register the Receiver in the manifest. Is you're Service running in a separate process? Is your Activity running in the foreground at the time of broadcast? – Mike M. Nov 09 '15 at 22:46
  • A extension of it at least. All my activities inherit from that activity. Could that be the problem? – Ryan McCaffrey Nov 10 '15 at 14:50

1 Answers1

0

turns out a couple things where wrong. First exported needed to be true in the manifest. Second, because the service is an Intent service it operates in a different thread than the main activities, so in order to send a broadcast to them I have to make it look something like:

Handler mHandler = new Handler(getMainLooper());
        mHandler.post(new Runnable() {
             @Override
             public void run() {
                  Intent newintent = new Intent("push-message");
                  newintent.putExtra("message", message);
                  LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(newintent);
             }
         });

So the main thread is hit. I hope this helps someone else with the same problem. I'd like to credit rubenlop88 in the for posting this solution for a similar problem in the thread, java.lang.RuntimeException: Handler (android.os.Handler) sending message to a Handler on a dead thread

Community
  • 1
  • 1
Ryan McCaffrey
  • 201
  • 2
  • 15