0

I want to turn on airplane mode through the code and turn off bluetooth, wifi and mobile data. First, execute this in the console:

adb shell pm grant com.package.name android.permission.WRITE_SECURE_SETTINGS

Second, turn on airplane mode from code:

//Set radios cell,wifi,bluetooth witch will be off
Settings.Global.putString(requireContext().contentResolver,
   Settings.Global.AIRPLANE_MODE_RADIOS,
   "${Settings.Global.RADIO_CELL},
   ${Settings.Global.RADIO_WIFI},
   ${Settings.Global.RADIO_BLUETOOTH}"
)

Settings.Global.putInt(
    requireContext().contentResolver,
    Settings.Global.AIRPLANE_MODE_ON,  1
)

But in the settings, I see that only wifi is turned off, bluetooth and mobile date are still on. Why can't I turn them all off from the code?

1 Answer 1

0

First of all, some android manifactures require usage of Settings.Secure instead of Settings.Global. Check it via command

adb shell settings list global

Second of all, you should try to notify system about your changes.

adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

There is parameter for this intent that is unneccessary to use on almost every device to notify specific airplane state (1 - ON, 0 - OFF):

adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state <state> 
5
  • Of course, you can do it programmatically with Intent. Apr 8, 2022 at 8:29
  • I can't do it from code because I am getting java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=27135, uid=10138. This happens because WRITE_SETTINGS system permissions (available only for system apps) are needed. Similar problem on Stackoverflow: stackoverflow.com/q/22349928/16343477 Apr 8, 2022 at 10:38
  • But you somehow managed to get WRITE_SECURE_SETTINGS permission. Then I suppose it's possible to get a little more:) Apr 8, 2022 at 14:10
  • WRITE_SECURE_SETTINGS can be granted via adb (see command in description), but it is not working for WRITE_SETTINGS permission. I am getting: Exception occurred while executing 'grant': java.lang.SecurityException: Permission android.permission.WRITE_SETTINGS requested by com.physiq.monitor.mdm.watch is not a changeable permission type Apr 11, 2022 at 6:32
  • Then it's root-only feature, okay, that's unfortunate. I've seen some OEMs that can change airplane mode without intent. If you are still searching, there is no workaround with kiosk apps or DeviceOwner|ProfileOwner Apr 12, 2022 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.