You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android代码实现飞行模式开关:root设备适配问题求助

开启/关闭Android飞行模式的Root代码兼容性问题

已申请权限

在AndroidManifest.xml中已添加以下权限:

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

测试代码及问题

我用以下代码尝试通过Root权限控制飞行模式:

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) {
                
}

这段代码在**已Root的魅族5(Android 6.0)上可以正常运行,但在已Root的三星S4(Android 5.0.1)和阿尔卡特5033D(Android 8.1.0)**上完全无效。

另外,通过电脑ADB执行以下命令时,三星S4可以正常切换飞行模式:

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

已尝试的无效方案

  1. 将两个命令合并执行,试过两种写法:

    // 写法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"
    

    两种方式测试后均无效果。

  2. 分两次调用exec执行命令:

    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" }
    };
    // 也试过第二个命令不加su的版本,同样无效
    // 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) {
    }
    

    这种方式同样没有达到预期效果。

内容的提问来源于stack exchange,提问作者Kadagor Mr.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 18:05:32