1

It's probably a very simple question but I seem to be hung up on these from a day. So here is the question:

How to unregister a BroadcastReceiver in Activity which was registered from AndroidManifest file? Here is the code::

 <receiver  android:name=".PhoneCallReceiver">
        <intent-filter  >
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

I am familiar with the function

public void unregisterReceiver (BroadcastReceiver receiver)

but it throws an exception if not registered from that Activity with

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
registerReceiver(mybroadcast, filter);
Jaydev
  • 1,794
  • 20
  • 37
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
  • Try unregistering it from the application object. I think that receivers in the manifest are registered in the application. – Gabe Sechan Feb 11 '13 at 06:00
  • @GabeSechan-I dont get it...can u provide me with some code..?? – Name is Nilay Feb 11 '13 at 06:01
  • getApplication().unregisterReceiver(receiver); The Application object also allows you to register a receiver and unregister it. If you register a receiver via the manifest it becomes registered with the application context, not an activity context. – Gabe Sechan Feb 11 '13 at 06:04
  • @NilayOnAndroid : see [Android - how to unregister a receiver created in the manifest?](http://stackoverflow.com/questions/6529276/android-how-to-unregister-a-receiver-created-in-the-manifest) post – ρяσѕρєя K Feb 11 '13 at 06:05
  • @GabeSechan-It throws "me:java.lang.IllegalArgumentException: Receiver not registered" exception !!! – Name is Nilay Feb 11 '13 at 06:07
  • What would happen if I do not unregister the broadcast receiver? Would it remain active even after the phone reboots ? – Jaydev Nov 15 '16 at 05:27
  • @Jaydev, no it will not remain active after phone reboots. But android will give you warning to unregister it immediately after that activity's onPause is called. This is generally the best practice to follow. – Name is Nilay Nov 15 '16 at 05:48

2 Answers2

4

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);
iTech
  • 18,192
  • 4
  • 57
  • 80
  • @iTech- Dont I need to mention which action receiver I need to unregister...for Eg: Here it is 'android.intent.action.PHONE_STATE' !! – Name is Nilay Feb 11 '13 at 06:20
  • You are specifying the receiver class `PhoneCallReceiver.class` which is sufficient to determine which receiver to disable – iTech Feb 11 '13 at 06:24
0

Try this:

public void onResume()
  {
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.intent.action.PHONE_STATE");
    registerReceiver(mybroadcast, filter);  

  }

  public void onPause()
  {
    unregisterReceiver(mybroadcast);
  }
user1744952
  • 508
  • 1
  • 3
  • 15