2

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!

strange quark
  • 5,205
  • 7
  • 41
  • 53

1 Answers1

7

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.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59