1

I have declared my receiver in my manifest:

<receiver
    android:name=".MyTestReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.ACTION_TEST"/>
    </intent-filter>
</receiver>

And here is my MyTestReceiver class:

public class MyTestReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if ("com.example.ACTION_TEST".equals(action)) {

            Toast.makeText(context, "Test!", Toast.LENGTH_SHORT).show();

        }

    }
}

But when I execute this code from elsewhere within my app:

Intent intent = new Intent("com.example.ACTION_TEST");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

...the local broadcast is not received (i.e., the toast is not shown).

Questions:

  1. Is it possible to register a local broadcast receiver in the manifest?
  2. If so, have I declared my local broadcast receiver incorrectly?
  3. If it's not possible to declare a local broadcast receiver in the manifest, and I declare it in my Application subclass instead, will it have the same 'scope' as a receiver declared in the manifest? (I mean, will it receive broadcasts in all the same conditions/situations as it would if it was declared in the manifest?)
  4. If there is a difference between specifying the receiver in the manifest than in my Application subclass, would I need to use general (non-local) broadcasts rather than local broadcasts? (In the actual app, the local broadcast will be sent when my IntentService completes its work. The IntentService will be triggered by an FCM push message.)

NB - All I can seem to find about this in the documentation is:

Note: To register for local broadcasts, call LocalBroadcastManager.registerReceiver(BroadcastReceiver, IntentFilter) instead.

...which doesn't address the main issue of whether or not you can specify the receiver in the manifest.

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • 2
    BroadcastReceiver from Manifest and Local Broadcast are being used for two different purpose. While looking into your manifest declaration, it seems you are trying to listen action from outside of application also. So here, you can not do it. – Pankaj Kumar May 23 '17 at 13:53
  • Possible duplicate of [Register a Local BroadcastReceiver in AndroidManifest.xml?](https://stackoverflow.com/questions/23364668/register-a-local-broadcastreceiver-in-androidmanifest-xml) – mike47 Dec 06 '18 at 02:42

1 Answers1

3

Is it possible to register a local broadcast receiver in the manifest?

No.

and I declare it in my Application subclass instead, will it have the same 'scope' as a receiver declared in the manifest? (I mean, will it receive broadcasts in all the same conditions/situations as it would if it was declared in the manifest?)

Well, no. A manifest-registered receiver is for system broadcasts, originating from any process. LocalBroadcastManager is local, for "broadcasts" within your own process.

You are welcome to register a receiver with LocalBroadcastManager in your Application (e.g., in its onCreate()), but I suspect that there are better solutions for whatever problem you are trying to solve.

In the actual app, the local broadcast will be sent when my IntentService completes its work

Then the receiver should be registered in the activity or fragment that needs to know about that work being completed. Your Application is unlikely to need to know about that work being completed, as if it did, your IntentService could just call a method on the Application and bypass all this broadcast stuff.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • What will actually happen in my receiver's `onReceive()` method is it will just show a notification. Therefore, it isn't tied to any activity (well, until the user opens the notification - but the activity won't be created until that later point, anyway). So is my `Application` the best/only place to register the receiver? – ban-geoengineering May 23 '17 at 13:49
  • @ban-geoengineering: "What will actually happen in my receiver's onReceive() method is it will just show a notification. " -- do that from the `IntentService`, and get rid of all the broadcast stuff. "So is my Application the best/only place to register the receiver?" -- the best thing to do is to delete the receiver entirely. – CommonsWare May 23 '17 at 13:52
  • Thank you, yes, that makes sense. I've taken this approach, as the local broadcast is also used by other components (e.g., active activities to update their UI), so I was looking for another way to listen to the same local broadcast at all times, application-wide. Also, another part of the thinking to use the `Application` subclass to listen for the local broadcast was to allow the `IntentService` just to do it's thing (i.e., just get data from the server) then let another component do the UI stuff (i.e., the `Application` subclass showing the notification). Would that be poor design? – ban-geoengineering May 23 '17 at 14:03
  • @ban-geoengineering: "Would that be poor design?" -- I'd say it is overkill. I don't know why you think an `Application` is somehow more appropriate for displaying a `Notification` than is an `IntentService`. – CommonsWare May 23 '17 at 14:06
  • I would just like to separate my workhorse (`IntentService`) code from my UI code. The `Application` would just be used to register the local broadcast receiver (because I can't register it in the manifest). The local broadcast receiver itself could be a separate class, with the notification displayed from there. – ban-geoengineering May 23 '17 at 14:12