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!
You can use
PackageManager#setComponentEnabledSetting
to enable/disable a component in your manifest file. You create aComponentName
with your package name and class name of your broadcast receiver. Then use theCOMPONENT_ENABLED_STATE_DISABLED
flag to disable it. And depending on if you want the entireApplication
object to die or not use theDONT_KILL_APP
flag or 0. Though the documentation warns against not killing theApplication
.