Android - how to unregister a receiver created in the manifest?
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.
Use this to create a Component
ComponentName component = new ComponentName(context, MyReceiver.class);
Check if the Component is enabled or disabled
int status = context.getPackageManager().getComponentEnabledSetting(component);
if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
Log.d("receiver is enabled");
} else if(status == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
Log.d("receiver is disabled");
}
Enable/Disable the component(Broadcast Receiver in your case)
//Disable
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
//Enable
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
Unregistering broadcast receiver defined in Android manifest
I have defined the receiver in MainActivity (as I want the receiver to be executed even when the activity is not in the foreground).
Since proximityOffReceiver
is a static inner class, the fact that you have it inside MainActivity
does not mean that "when the activity is not in the foreground" affects proximityOffReceiver
.
But I am confused if I should explicitly unregister the receiver, each time when the application is quit or when the back button is pressed?
The accepted answer on that question points out that you do not "unregister" a receiver that is registered in the manifest. You enable or disable the receiver.
Hence, in your case, you enable the receiver when you want it operating, and you disable the receiver when you do not want it operating. In your case, it would appear that you want it operating all of the time, in which case you probably never disable it.
unregister broadcast receiver registered through manifest
You can disable Receiver with this code:
PackageManager pm = getPackageManager();
ComponentName compName =
new ComponentName(getApplicationContext(),
MyReceiver.class);
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Also you can use COMPONENT_ENABLED_STATE_ENABLED
to enable Receiver.
Android Broadcastreceiver unregister needed when registered in manifest?
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
How to unregister BroadcastReceiver from AppWidgetProvider?
Solution:
Answer. Yes, registering a BroadcastReceiver in onUpdate(), will register a new one every time. I solved the issue of creating too many Broadcastreceivers by creating one in OnEnabled(), as that code only runs when the first widget is created.
Here's how I'm planning to unregister the broadcastReceiver I created in onEnabled. I will add the intent filter:ACTION_APPWIDGET_DISABLED to the broadcastReceiver when registering it. In the OnReceive() method of the custom broadcastReceiver I will execute:
@Override
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals("ACTION_APPWIDGET_DISABLED"){
context.unregisterReceiver(this);
}
else{
...
//Rest of my method
{
{
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.
Unregister receiver in Manifest xml file
if declaring in manifest you dont need to do anything
How to unregister BroadcastReceiver from Activity
To disable a receiver
that was defined in the manifest
, use the following:
PackageManager pm = context.getPackageManager();
ComponentName component = new ComponentName(context, PhoneCallReceiver.class)
pm.setComponentEnabledSetting(component , PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
How to unregister BroadcastReceiver
Edit:
For an Activity:
In order to register your broadcast receiver from within your app, first, remove the <receiver>
tag from your AndroidManifest.xml file. Then, call registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
in your onResume()
. Use unregisterReceiver(BroadcastReceiver receiver)
in your onPause()
to unregister the Broadcast receiver.
For a Service:
Remove the receiver tag from the manifest file. You can then register your Broadcast receiver with the same method in the onCreate()
and unregister in the onDestroy()
.
EDIT: Sample Code:
public class MyActivity extends Activity {
private final BroadcastReceiver mybroadcast = new SmsBR();
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mybroadcast, filter);
}
public void onPause() {
super.onPause();
unregisterReceiver(mybroadcast);
}
}
Related Topics
What's "Tools:Context" in Android Layout Files
How to Get the APK of an Installed App Without Root Access
Listview Scroll to the End of the List After Updating the List
Best Way to Add Activity to an Android Project in Eclipse
Android: Force Edittext to Remove Focus
How to Compile the Android Aosp Kernel and Test It with the Android Emulator
Resolved Versions for App (22.0.0) and Test App (21.0.3) Differ
Connectivitymanager Getnetworkinfo(Int) Deprecated
Android Custom Dropdown/Popup Menu
Android Ratingbar Change Star Colors
Notifydatasetchange Not Working from Custom Adapter
Change the Color of a Checked Menu Item in a Navigation Drawer
How to Do a Simple Search in String in Firebase Database
Broadcast Receiver Within a Service
How to Strike Through Text in an App Widget
Software Keyboard Resizes Background Image on Android