Dave Dave - 11 months ago 35
Android Question

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

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.


Answer Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download