So mainly that is the question, when I register a Broadcastreceiver dynamically, it is needed to be unregistered at onDestroy. But when the Broadcastreceiver is registered within the manifest, do we need to unregister it?
- 313
- 3
- 19
2 Answers
You cannot unregister a manifest-registered receiver. If the receiver is enabled, it will always be listening for broadcasts.
- 986,068
- 189
- 2,389
- 2,491
-
kindly please, can u tell me how to set the manifest-registered receiver to be disabled? i tried android:enabled="false" and in onStrat() i checked if the receiver is enabled or not, but i found that it is always in the COMPONENT_ENABLED_STATE_DEFAULT β Amrmsmb Aug 29 '15 at 11:53
-
1@user2121: Well, `android:enabled="false"` should work. You can change it programmatically by calling `setComponentEnabledSetting()` on `PackageManager`. β CommonsWare Aug 29 '15 at 12:28
You should be careful while adding broadcast receiver because unnecessary broadcast receivers drain battery power.
If you add the broadcast receiver in the Android manifest file, itβs implied that you are going to handle a particular intent in the broadcast receiver and not ignore it.
You can use the PackageManager to enable/disable a BroadcastReceiver in declared in the Manifest. The Broadcast Receiver will get fired only when it is enabled.
For more info see Android - how to unregister a receiver created in the manifest?
There is a way to enable and disable the broadcast receiver which is added in the manifest file.
See this post Android broadcast receiver: Registering/unregistering during runtime
-
True about the battery! So yes, I am using one with a Bluetooth connection, but my app has several activities. When registering in a dynamical way it seems to stop when other activity than the main one starts, but when registered in manifest it works perfect. But it keeps on even after `onDestroy` happens. I was planning to disable them at `onDestroy`. Your links are quite useful by the way! β gkapellmann Oct 17 '14 at 16:34