Broadcast Receiver Register in Manifest VS. Activity

Broadcast Receiver Register in Manifest vs. Activity

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. For some broadcasts, it doesn't really make sense to create a new app process if it doesn't exist, or there are some security, performance, etc. implications, and thus you can only register the receiver in code.

As for the HEADSET_PLUG broadcast, it seems the idea is that your already running app can get this to do app-specific adjustments to UI, volume, etc. If your app is not running, you shouldn't really care about the headphones being unplugged.

AFAIK, there is no single place this info is summarized for all broadcasts, but each Intent should have a comment in the JavaDoc about how to register and use it, but apparently it's lacking in places. You should be able to compile a list if you grep the Android source tree for Intent.FLAG_RECEIVER_REGISTERED_ONLY.

Register Broadcastreceiver in manifest

OK. After banging my head against the wall for more than a week, I decided to create a new project with the minimum functionality and it worked from the first try. I don't know whether it was caused by something in my old code, which I doubt, since I copied it method by method to the new project and it still works, or the newer versions of Android Studio and their build tools.

Huge thanks to REG1 for having the patience and desire to give ideas.

Regards

BroadcastReceiver not called when registering from manifest

Since Android Oreo, receivers must be registered in runtime using

context.registerReceiver(receiver, intentFilter);

to receive implicit intents

You can still receive explicit intents and some special implicit actions, as boot_completed or locale_changed for example

More information look below link

https://developer.android.com/about/versions/oreo/background.html#broadcasts

What is the difference between registering BroadcastReceiver in code and in manifest?

what is the difference registering receiver through activity and in android manifest?

Receivers declared in manifest always active, registered in activity - active only after registration and will die with app process.

can service be started from manifest, without call startService() from code?

No

Start all services inside BootFinishedReceiver (or whatever you called it)

Also, it's good practice to register only one receiver for all broadcasts.

How to register broadcast receiver from Manifest?

I want to create a background app with no UI.

That is not practical.

But when I run the app the broadcast receiver is never being registered. When I register my receiver from a test activity, it works fine. Why could this be happening?

Manifest-registered receivers do not work upon installation. They only work after something uses an explicit Intent to start up one of the app's components. For 99.9% of apps, that is their home screen launcher activity.

is the broadcast receiver registered via manifest is automatically unregister on app close?

Android BroadcastReceiver declared in the manifest are not automatically unregistered. Those can be triggered even when the app is not running (that is the expected behavior too). When a BroadcastReceiver is triggered, the onReceive callback method is called. The broadcast is only active during the execution of the onReceive method. Once the method is returned, the receiver is considered as inactive. So, if you want to declare a BroadcastReceiver which can be triggered always (even when the app is not running), you declare it in the manifest. And you don't have to worry about its lifecycle.

However, if you want to declare a broadcast which will receive broadcast only during a certain amount of time (e.g., when app is running or when an activity is running), then it is better to register the broadcast when needed and unregister it when the work is done. For example, if you want to receive a broadcast only when an activity is running, then you can register in in the onCreate method and unregister it in the onDestroy method.

Using a broadcast receiver as an activity innerclass with manifest registration

If UpdateUIClass is an internal class of MyActivity then you need to refer to it this way android:name=".MyActivity$UpdateUIClass"

This is the way you do it in a layout XML. Not sure if this will work for the manifest but give it a go and see if it works.

Edit

This is not possible unless the inner class is static as the manifest marshals all objects at load and not on demand. The class hosting the sub-class does not exist at the time the receiver is being resolved.

Related question: Is it possible to define a broadcast receiver as an inner class in manifest file?



Related Topics



Leave a reply



Submit