0

I making a app in which a broadcast receiver run regularly even when application is close. My app is working very well but when i want to unregister my Broadcast Receiver it gave me error that "receiver is not registered". I write its entry on the manifest file as this

<receiver  android:name=".PhoneCallReceiver">
            <intent-filter  android:priority="10" >
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
J. Steen
  • 15,470
  • 15
  • 56
  • 63

2 Answers2

2

You cannot use unregisterReceiver() to unregister something that you registered in the manifest. Use PackageManager and setComponentEnabledSetting() to indicate whether this <receiver> is enabled or not -- if it is not enabled, it will not longer respond to broadcasts.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

See this answer: https://stackoverflow.com/a/6529365:

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);
Community
  • 1
  • 1
QArea
  • 4,955
  • 1
  • 12
  • 22