如何开启/关闭飞行模式,即使是在新的Andr​​oid版本(甚至用根)? [英] How to turn on/off airplane mode, even on new Android versions (and even with root)?

查看:509
本文介绍了如何开启/关闭飞行模式,即使是在新的Andr​​oid版本(甚至用根)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Android 4.2开始,/关闭飞行模式开启不使用正常的API支持。

Starting with Android 4.2 , turning on/off airplane mode isn't supported using normal APIs.

当WRITE_SECURE_SETTINGS权限授予它应该工作,但是这只是系统的应用程序(如我读过)。

It should probably work when WRITE_SECURE_SETTINGS permission is granted, but that's only for system apps (as I've read).

我应该以同根做的设备来完成?

What should be done in order to do it on devices with root?

如果一个系统的应用还需要根以切换飞行模式?

Should a system app also require root in order to toggle airplane mode?

推荐答案

要切换的固定翼/飞行模式和关闭在Android 植根设备(手机,平板电脑,注),你可以执行以下操作:

To toggle Airplane / Flight mode on and off on an Android rooted device (phone, tablet, note), you can do the following:

private final String COMMAND_FLIGHT_MODE_1 = "settings put global airplane_mode_on";
private final String COMMAND_FLIGHT_MODE_2 = "am broadcast -a android.intent.action.AIRPLANE_MODE --ez state";

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public void setFlightMode(Context context) {
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        // API 17 onwards.
        if (isRooted(context)) {            
            int enabled = isFlightModeEnabled(context) ? 0 : 1;
            // Set Airplane / Flight mode using su commands.
            String command = COMMAND_FLIGHT_MODE_1 + " " + enabled;
            executeCommandWithoutWait(context, "-c", command);
            command = COMMAND_FLIGHT_MODE_2 + " " + enabled;
            executeCommandWithoutWait(context, "-c", command);
        } else {                
            try {
               // No root permission, just show Airplane / Flight mode setting screen.
               Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
               intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
               Log.e(TAG, "Setting screen not found due to: " + e.fillInStackTrace());
            }
        }
    } else {
        // API 16 and earlier.
        boolean enabled = isFlightModeEnabled(context);
        Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enabled ? 0 : 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", !enabled);
        context.sendBroadcast(intent);
    }

要检查是否固定翼/飞行模式已经开启和关闭,请执行以下操作:

To check whether Airplane / Flight mode is already on and off, do the following:

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private boolean isFlightModeEnabled(Context context) {
    boolean mode = false;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        // API 17 onwards 
        mode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
    } else {
        // API 16 and earlier.
        mode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    }
    return mode;
}

要执行指令,请执行以下操作:

To execute su command, do the following:

private void executeCommandWithoutWait(Context context, String option, String command) {
    boolean success = false;
    String su = "su";
    for (int i=0; i < 3; i++) {
        // "su" command executed successfully.
        if (success) {
            // Stop executing alternative su commands below. 
            break;
        }
        if (i == 1) {
            su = "/system/xbin/su";
        } else if (i == 2) {
            su = "/system/bin/su";
        }       
        try {
            // execute command
            Runtime.getRuntime().exec(new String[]{su, option, command});
        } catch (IOException e) {
            Log.e(TAG, "su command has failed due to: " + e.fillInStackTrace());
        }   
    }
}

另外,如果您的应用程序:

Alternatively, if your app:

  1. 在与一位Android框架的证书签名;和
  2. 在安装到 /系统/应用/ 目录;和
  3. 您也可以在的Andr​​oidManifest.xml 申报文件中的相关标记(如 WRITE_SECURE_SETTINGS 等)。
  1. Was signed with an Android framework's certificate; and
  2. Was installed to the /system/app/ directory; and
  3. Have the relevant tags declared in AndroidManifest.xml file (e.g. WRITE_SECURE_SETTINGS, etc).

那么你可以这样做:

Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

由于 Settings.Global 定义什么都可以读写通过系统的应用程序 - 甚至创建为系统应用程序的第三方应用程序

Since anything defined in Settings.Global can be read-write by system apps - even third-party app created as a system app.

这篇关于如何开启/关闭飞行模式,即使是在新的Andr​​oid版本(甚至用根)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆