Android 在版本 >=4.1 时禁用飞行模式

2024-02-29

我需要在我的信息亭应用程序中禁用飞行模式,我尝试下面的代码片段来覆盖设备设置

try {
    int airplane = Settings.System.getInt(getApplicationContext().getContentResolver(), "airplane_mode_on");
    if (airplane == 1) {
        getApplicationContext().sendBroadcast(new Intent("android.intent.action.AIRPLANE_MODE").putExtra("state", false));
    }
} catch (SettingNotFoundException e) {
    e.printStackTrace();
}

该代码似乎适用于 android 版本 4.0,而在 4.1 及更高版本中则不起作用。我希望通过 root 设备访问系统设置来使其工作。 实际上,我的任务是从 Nexus 平板电脑的状态栏中禁用飞行模式功能。让我知道有关这些实施的任何建议。


对于 Android

static boolean getAirplaneMode(Context context)
{
   return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}


static void setAirplaneMode(Context context, boolean mode)
{
   if (mode != getAirplaneMode(context))
   {
      Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, mode ? 1 : 0);
      Intent newIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
      newIntent.putExtra("state", mode);
       context.sendBroadcast(newIntent);
    }
}

对于 Android 4.2 看看在 Android 4.2(及更高版本)上修改 AIRPLANE_MODE_ON https://stackoverflow.com/questions/13422527/modify-airplane-mode-on-on-android-4-2-and-above/15448650#15448650

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 在版本 >=4.1 时禁用飞行模式 的相关文章

随机推荐