Search code examples
androidbroadcastreceiverintentfilter

Retrieve instance of a registered BroadcastReceiver to unregister


in my app I have 2 BroadcastReceivers. Lets call the AReceiver and BReceiver. AReceiver is just listening for android.intent.action.BOOT_COMPLETED so it can register BReceiverfor a different action, if its turned on in the settings of my app.

So I have a PreferenceActivity that can switch the status of my app, which means:

  • Status: On
    • AReceiver will not register BReceiver at boot
    • Changing from Off -> On should register BReceiver
  • Status: Off
    • AReceiver will register BReceiver at boot
    • Changing from On -> Off should unregister BReceiver

Here is the problem I'm having. How can I unregister BReceiver if it was either registered in AReceiver or in the PreferenceActivity, which could have been closed in the meanwhile? I don't know what I have to pass to unregisterBroadcastReceiver() as an argument, since it wants the instance of the registered receiver.

How do I retrieve the instance of a registered BroadcastReceiver so I can unregister it?


Solution

  • You should be able to put the BReceiver in the manifest (default disabled) and then enable and disable it at any time. Have a look here