Is it better code practice to register a receiver in manifest or in code?

前端 未结 4 1618
北恋
北恋 2021-01-18 07:19

I\'m writing a simple broadcast receiver. I\'ve registered receivers in both the manifest and in the code before. For my purposes this is a simple receiver that doesn\'t nee

相关标签:
4条回答
  • 2021-01-18 08:00

    It depends on scenario.

    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:

    1. Your app offers some kind of service around these events

    2. 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.

    0 讨论(0)
  • 2021-01-18 08:02

    I can't speak as to the efficiency of the implementation of one over the other (my intuition tells me that it's too close to really matter), but for reasons hinted at in Cristian's answer, registering and unregistering programmatically might make your app more efficient.

    If you register in the manifest, your broadcast receiver will always be woken up by any intents that match your filters. If you register programmatically, you can only allow your receiver to be woken up at particular times, and you can control which intents will wake up your receiver and at which times.

    If you're really worried about waking up the receiver at times that it doesn't need to be, then do it programmatically in the code. You'll need to be more careful to always unregister, and ensure that your receiver is registered at all times that you expect it to be, but if you do that correctly, you can avoid waking up your receiver unnecessarily, and thus saving some efficiency.

    0 讨论(0)
  • 2021-01-18 08:11

    Simply put

    Dynamic Registration - Your app expects something to happen immediately while the app is running

    Static Registration - You app is waiting for something to happen over the long term. And since you cannot guarantee your app will be running when it happens you can politely ask the android system to notify you when it does

    They both would have the same execution beyond that point

    0 讨论(0)
  • 2021-01-18 08:13

    Well, they are actually different. You seem to think that it is almost same. When you register a receiver in code, you must unregister it when the app gets destroy (actually, when the Activity or Service that register it, gets destroy). On the other hand, when you declare it in the manifest, you make it available even if you app is not running.

    Just ask your self: which of the two approaches best fits your needs?

    0 讨论(0)
提交回复
热议问题