7

I'm trying to use a local broadcast receiver.

In order to do so I"ve done the next steps -

1) At an Activity, where Iwould like something to happen, I've created a class -

private class NewGroupReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("The group ", "GOT IN THE RECIVING");
        Toast.makeText(this, "Working",Toast.LENGTH_SHORT).show();  
    }

}

2) At the same activity I've used the next code in order to create a receiver -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    NewGroupReceiver receiver = new NewGroupReceiver();

    //the intent filter will be action = "com.example.demo_service.action.SERVICE_FINISHED"
    IntentFilter filter= new IntentFilter("com.example.apps.action.NEW_GROUP");

    // register the receiver:
    registerReceiver(receiver, filter);
}

3) At the a service class I've used the next code to know when something has happened-

Intent resultsIntent=new Intent("com.example.apps.action.NEW_GROUP");

LocalBroadcastManager localBroadcastManager =LocalBroadcastManager.getInstance(this);

localBroadcastManager.sendBroadcast(resultsIntent);

Now the problem is that when the thing I WOuld like to know has happen - I see the it's get into the code that I've used at step 3, but it doesen't seem to get into the BroadcastReceiver - the step 1 code.

Any idea what am I doing wrong here? Thanks for any kind of help.

Sully
  • 14,672
  • 5
  • 54
  • 79
4this
  • 759
  • 4
  • 13
  • 27

1 Answers1

14

You are using the LocalBroadcastManager to send the request, but you register the receiver on the "global" Intent. You should either use LocalBroadcastManager to register the receiver or send the broadcast on the application context:

Step 2

LocalBroadcastManager.getInstance(this).registerReceiver (receiver, filter);
JohnnyAW
  • 2,866
  • 1
  • 16
  • 27
  • Can you reference documentation confirming that this is true? I.e. that a broadcast sent via the LocalBroadcastManager will not be received by a BroadcastReceiver that was registered via `Context.registerReceiver()`, even though that BroadcastReceiver is in the same app? I've been looking for confirmation of this but the docs don't really seem to say. – LarsH Jul 11 '17 at 21:02
  • 1
    @LarsH unfortunately not, but that was the case from my experience back then in 2014 – JohnnyAW Aug 13 '17 at 12:19