Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Cheers

like image 308
Dave Avatar asked Oct 24 '11 19:10

Dave


People also ask

How do I register my manifest receiver?

There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in code and register it with the Context. registerReceiver() method.

How many ways can you register a broadcast receiver?

A BroadcastReceiver can be registered in two ways. By defining it in the AndroidManifest. xml file as shown below.

Where is broadcast receiver registered in activity?

This example demonstrates how do I register a BroadcastReceiver programtically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which statement is true about a broadcast receiver that is registered in manifest XML file?

If your receiver is registered in the manifest, and your app is not running, a new process will be created to handle the broadcast. If you register it in code, it's tied to the life of the activity/service you registered it in.


2 Answers

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?

like image 109
Cristian Avatar answered Oct 06 '22 00:10

Cristian


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.

like image 20
Chris Bye Avatar answered Oct 06 '22 00:10

Chris Bye