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

寵の児 提交于 2019-12-01 16:09:47

问题


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 need to do anything fancy.

Is there a reason to choose one method over the other in this case? Is registering the receiver in the manifest more efficient (executes faster)? Or are they both basically the same?

I'm asking because the application I'm writing needs to be very efficient, and I haven't been able to find good information on the practical difference between the two methods. I'm trying to follow whatever is the best coding practice.

Cheers


回答1:


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?




回答2:


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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/7880741/is-it-better-code-practice-to-register-a-receiver-in-manifest-or-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!