Android - How to use a local broadcast receiver?

I am trying to use a local broadcast receiver.

To do this, I took the following steps -

1) In Activity, where I would like something to happen, I 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) With the same activity, I used the following code 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) In the service class, I used the following code to find out when something 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 wanted to know about happened - I see that it falls into the code that I used in step 3, but it does not seem to fall into the BroadcastReceiver - step 1.

Any idea what I'm doing wrong here? Thanks for any help.

+4
source share
1 answer

LocalBroadcastManager , "" . LocalBroadcastManager :

2

LocalBroadcastManager.getInstance(this).registerReceiver (receiver, filter);
+11

Source: https://habr.com/ru/post/1538454/


All Articles