Search code examples
androidbroadcastreceiverboot

Register / Unregister boot broadcast receiver


I registered a boot broadcast receiver according to this post:

Launch activity using boot broadcast receiver

And I'm wondering how to unregister it? / can it register/unregister programmatically?

The problem is that the register was done in the manifest and not programmatically.


Solution

  • If you register a BroadcastReceiver in the Manifest, it will be triggered even if the app is not running. If you register a BroadcastReceiver in an Activity, then it will be triggered only when the code is running and it should be unregistered when the acitivty is inactive.

    Since this is a BOOT_COMPLETED broadcast, registering in the codes doesn't make sense. It should be registered in the Manifest, and I don't think we can unregister it. However, we can ignore it by checking some boolean variables from the SharedPreferences or a file inside the onReceive() method, etc. Like:

    if(condition){//use the variables to verify the condition
    //do something
    }