Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - how to unregister a receiver created in the manifest?

I know about using registerReceiver and unregisterReceiver in Java code for dealing with receivers, but let's say I have the following in my manifest:

    <receiver android:name=".headsetHook">
        <intent-filter android:priority="99999999999">
            <action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
        </intent-filter>
    </receiver>

Is there a way I could unregister this somewhere in Java code? Could I give it an id attribute or something and then get it and unregister it? I ask because I want my app to do something only on the first time this action happens, then unregister it and re-register it later in Java.

Hope I made that clear, thanks for any help.

like image 808
JDS Avatar asked Jun 30 '11 02:06

JDS


People also ask

How do I unregister a broadcast receiver?

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() .

Do we need to unregister broadcast receiver?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

What is receiver in Android manifest?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.


2 Answers

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);
like image 110
Varun Avatar answered Oct 02 '22 04:10

Varun


Based on Varun's answer I've created this utilitarian method

private void unregisterReceiverFromManifest(Class<? extends BroadcastReceiver> clazz, final Context context) {
    final ComponentName component = new ComponentName(context, clazz);
    final int status = context.getPackageManager().getComponentEnabledSetting(component);
    if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
        context.getPackageManager()
                 .setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                     PackageManager.DONT_KILL_APP);
        }
    }
like image 43
hzitoun Avatar answered Oct 02 '22 05:10

hzitoun