问题
I have a BroadcastReceiver set up in my Android application that receives SMS receive events. This works fine, but I want to be able to toggle SMS receiving on and off by toggling the BroadcastReceiver on and off. Because if I have a simple boolean inside the onReceive method, even if the SMS receiving is off, my application will start.
Is this possible to do?
Cheers!
回答1:
You can use PackageManager#setComponentEnabledSetting to enable/disable a component in your manifest file. You create a ComponentName
with your package name and class name of your broadcast receiver. Then use the COMPONENT_ENABLED_STATE_DISABLED
flag to disable it. And depending on if you want the entire Application
object to die or not use the DONT_KILL_APP
flag or 0. Though the documentation warns against not killing the Application
.
来源:https://stackoverflow.com/questions/3096246/set-broadcastreceiver-to-be-inactive