The main difference between Manifest and Programmatic BroadcastReceiver registration

I am trying to understand the main differences between registering a BroadcastReceiver in a manifest and its software programming ...

My understanding basically looks like this (I would like someone to correct my points if I am missing something).

  • Registered in the manifest:

    • The OS will magically find and instantiate your class, if necessary, by calling the onReceive () method, regardless of whether the state of your application is running
    • Your reception will be called only once for broadcasting (i.e. you can consider that registering in the manifest is similar to registering your "class" for receiving a broadcast - and broadcasting creates an instance of your class as necessary) (??)
  • Registered Software:

    • Registering in the code means that you register instances of your class to receive broadcast messages (i.e. if your code is a bit messy and you manage to register several times, you will get several BroadcastReceiver instances that have their onReceive () called for broadcast
    • To unregister, you need to unregister a specific BroadcastReceiver instance that you previously registered
    • If your application is destroyed by the OS, your onReceive () method will not be called for broadcast

thank

+17
android android-lifecycle broadcastreceiver
Sep 06 '10 at 13:20
source share
3 answers

This is mostly correct for you.

Note that the registered object receiver is used only once. A new instance of your BroadcastReceiver is created for each broadcast. The main use of registrars registered in manifests is for broadcasts that can continue until your code is in memory (for example, BOOT_COMPLETED , your scheduled alarms via AlarmManager ).

+17
Sep 06 2018-10-06
source share

When to use this method for registration

Which method to use to register your BroadcastReceiver depends on what your application is doing with the system event. I think there are two reasons why your application wants to learn about system-wide events:

  • Your application offers some kind of service around these events.

  • Your application wants to respond kindly to state changes.

Examples for the first category are applications that should work immediately after loading the device or start some work when installing the application. Battery Widget Pro or App2SD are good examples for such applications. For this type, you must register BroadcastReceiver in the manifest file.

Examples of the second category are events that signal a change in circumstances that your application can rely on. Say that your application depends on the established Bluetooth connection. You must respond to a state change - but only when your application is active. In this case, there is no need for a statically registered broadcast receiver. Dynamically registered would be more reasonable.

There are also several events for which you are not even allowed to statically register. An example of this is the Intent.ACTION_TIME_TICK event, which is broadcast every minute. This is a wise decision because the static receiver overcharged the battery.

+4
Dec 12 '13 at 5:23
source share

Your understanding is correct in my opinion.

Another important (and implicit) difference is that some specific system intentions will only trigger your receiver if it is registered programmatically . Receivers defined only in the manifest will not be called. Examples: ACTION_SCREEN_ON , ACTION_SCREEN_OFF , ACTION_BATTERY_CHANGED , ACTION_HEADSET_PLUG

I would recommend this text , which mentions various details about intentions and successors.

0
Nov 27 '13 at 18:47
source share



All Articles