Kadagor Mr.
Kadagor Mr.

Reputation: 1

How to programmatically enable airplane mode on Android?

I'm trying to programmatically enable/disable Airplane mode on Android.

Requested the following permissions in the manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

And using the following code:

String[] task = new String[] { "su", "settings put global airplane_mode_on 1", "am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true" };
try {
  Process process = Runtime.getRuntime().exec(task);
  process.waitFor();
} catch (IOException e) {
                
}catch (SecurityException e){
                
}catch (Exception e) {
                
}

This works correctly for rooted Meizu 5 (Android 6.0). But it doesn't work for rooted Samsung S4 (Android 5.0.1) and Alcatel 5033D (Android 8.1.0) :(

When I used adb commands connecting phone to computer it worked correctly for Samsung:

adb shell settings put global airplane_mode_on 1
adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

In some publics I saw advice in dividing the execution of a line in two stages:

  1. "settings put global airplane_mode_on 1; am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true" };
  2. "settings put global airplane_mode_on 1 && am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true" But it did not help.

Tried another way:

String[][] t = new String[][]{new String[] { "su", "settings put global airplane_mode_on 1"},new String[] {"su","am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true" }};
//String[][] t = new String[][]{new String[] { "su", "settings put global airplane_mode_on 1"},new String[] {"am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true" }};
 try {
  Process process = null;
  for (String[] s : t) {
    process  = Runtime.getRuntime().exec(s);
  }
  process.waitFor();
}catch (IOException e) {
}catch (SecurityException e){
}catch (Exception e) {
}

But it also did not bring a positive result.

Upvotes: 0

Views: 3805

Answers (1)

dvo
dvo

Reputation: 350

Without root access, one needs the "default assist app" privilege. It works for instance with the MacroDroid app - see also https://macrodroidforum.com/wiki/index.php/Action:_Airplane_Mode_On/Off.

Upvotes: 0

Related Questions