Local broadcast from the Service not received by type of activity

I have Activityone in which I register BroadcastReceiverlocally as follows:

public class SomeActivity extends Activity{

    public static final String PERFORM_SOME_ACTION = "PERFORM_SOME_ACTION";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.some_activity_layout);

        .....
        .....

        IntentFilter filter = new IntentFilter();
        filter.addAction(PERFORM_SOME_ACTION);

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // perform some action ...
            }
        };

        registerReceiver(receiver, filter);
    }

    .....
    .....
}

And I have Service, from which I broadcast Intentas follows:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {        

    Intent i = new Intent(SomeActivity.PERFORM_SOME_ACTION);
    sendBroadcast(i);   /* Send global broadcast. */

    return START_STICKY;
}

It works as intended. Having done this, I realized that local broadcasting would be more suitable for this situation:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {        

    Intent i = new Intent(SomeActivity.PERFORM_SOME_ACTION);
    LocalBroadcastManager.getInstance(this).sendBroadcast(i);   /* Send local broadcast. */

    return START_STICKY;
}

Unfortunately, the above diagram does not work. A global broadcast is sent every time, while a local broadcast seems to never be sent / not received.

What am I missing here? Is it impossible to send local translations between two separate application components, for example, two separate Activityor from Serviceto Activity? What am I doing wrong?

Note:

, ( -, ), ( inter-app, ) , . .

+4
1

?

LocalBroadcastManager LocalBroadcast, registerReceiver Activity, :

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

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


All Articles