简体   繁体   中英

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM