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?

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42

1 Answers1

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> 
  • Of course, you can do it programmatically with Intent. – Dmitry Pudov Apr 08 '22 at 08: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: https://stackoverflow.com/q/22349928/16343477 – AlexandraFilyakova Apr 08 '22 at 10:38
  • But you somehow managed to get WRITE_SECURE_SETTINGS permission. Then I suppose it's possible to get a little more:) – Dmitry Pudov Apr 08 '22 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 – AlexandraFilyakova Apr 11 '22 at 06: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 – Dmitry Pudov Apr 12 '22 at 10:12