开发者

How to set the AIRPLANE_MODE_ON to "True" or ON?

开发者 https://www.devze.com 2023-01-07 12:53 出处:网络
I a开发者_运维百科m planning to drop a call and I find this as one of workaround for that. How do I activate the airplane mode via code?

I a开发者_运维百科m 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.


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.


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


  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;
    }
  }


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" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号