4

I am planning to drop a call and I find this as one of workaround for that. How do I activate the airplane mode via code?

This way I will drop the call based on some event.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Prun
  • 8,203
  • 16
  • 60
  • 86
  • 2
    Please note that toggling airplane mode on/off is no longer possible as of Android 4.2 (Jelly Bean major release 1). This is because Google has adopted a BANDAGE solution to address shortcomings in Android instead of re-examining its security model. – ChuongPham Nov 24 '12 at 17:07

4 Answers4

15

See the blog article Android: Controlling Airplane Mode ,

Works only upto API 16

// Toggle airplane mode.
Settings.System.putInt(
      context.getContentResolver(),
      Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Post an intent to reload.
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);

where isEnabled is whether airplane mode is enabled or not.

RPB
  • 16,006
  • 16
  • 55
  • 79
Jess
  • 42,368
  • 6
  • 37
  • 51
1

Please keep in mind that this is no longer possible starting from Android 4.2 and above.

http://developer.android.com/reference/android/provider/Settings.Global.html#AIRPLANE_MODE_ON

Tommie
  • 1,945
  • 2
  • 14
  • 23
0
  public static boolean getAirplaneMode(Context context) {
      try {
      int airplaneModeSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
      return airplaneModeSetting==1?true:false;
    } catch (SettingNotFoundException e) {
      return false;
    }
  }
Ashok Domadiya
  • 1,124
  • 13
  • 31
0

You can do this:

        int mode = isEnable ? 1 : 0;
        Runtime.getRuntime().exec("settings put global airplane_mode_on "+mode);
        SystemClock.sleep(2000);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", isEnable);
        this.sendBroadcast(intent);

shell uid can change airplane_mode_on ,but send ACTION_AIRPLANE_MODE_CHANGED need permission (need to be system uid)

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Vic
  • 21
  • 2