I am trying to understand the main differences between registering a BroadcastReceiver in the Manifest and registering it programmatically…
My understanding is basically as follows (would appreciate someone correcting my points if I am missing something).
Registered in Manifest:
- The OS will magically find and instantiate your class if needed, calling the onReceive() method, regardless what the running state of your application was
- Your receive will only get called once per broadcast (i.e. You can consider that registering in the manifest is like registering your ‘class’ for receiving the broadcast – and the broadcast instantiates your class as needed) (??)
- registering in code means that you are registering instances of your class to receive broadcast messages (i.e. if your code is a little sloppy, and you manage to register several times, you will end up with multiple BroadcastReceiver instances all having their onReceive() called for a broadcast
- to unregister, you need to unregister the specific BroadcastReceiver instance that you previously registered
- if your application gets destroyed by the OS, your onReceive() method will not be called for a broadcast
You have it basically correct.
Note that a manifest-registered receiver object is only used once. A new instance of your
BroadcastReceiver is created for each broadcast. The primary use of manifest-registered receivers is for broadcasts that may go on while your code is not in memory (e.g.,
BOOT_COMPLETED, your scheduled alarms via
When to use which method to register
Which method to use for registering your BroadcastReceiver depends on what your app does with the system event. I think there are basically two reasons why your app wants to know about system-wide events:
Your app offers some kind of service around these events
Your app wants to react graciously to state changes
Examples for the first category are apps that need to work as soon as the device is booted or that must start some kind of work whenever an app is installed. Battery Widget Pro or App2SD are good examples for these kinds of apps. For this type you must register the BroadcastReceiver in the Manifest file.
Examples for the second category are events that signal a change to circumstances your app might rely on. Say your app depends on an established Bluetooth connection. You have to react to a state change – but only when your app is active. In this case there is no need for a statically registered broadcast receiver. A dynamically registered one would be more reasonable.
There are also a few events that you are not even allowed to statically register for. An example for this is the Intent.ACTION_TIME_TICK event which is broadcast every minute. Which is a wise decision because a static receiver would unnecessarily drain the battery.
Your understanding is correct according to mine.
Another relevant (and obscure) difference is that some specific system Intents will only trigger your receiver if it’s programmatically registered. Receivers only defined in the manifest won’t be called.
Examples are: ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_BATTERY_CHANGED, ACTION_HEADSET_PLUG
I’d recommend this text that mentions various details about Intents and Receivers.