Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打开/关闭飞行模式,即使在新的Android版本上(甚至使用root)?_Android_Root_Airplane - Fatal编程技术网

如何打开/关闭飞行模式,即使在新的Android版本上(甚至使用root)?

如何打开/关闭飞行模式,即使在新的Android版本上(甚至使用root)?,android,root,airplane,Android,Root,Airplane,从Android 4.2开始,使用普通API不支持打开/关闭飞行模式 当授予WRITE_SECURE_SETTINGS权限时,它可能会工作,但这仅适用于系统应用程序(如我所读) 要在具有root的设备上执行此操作,应执行哪些操作 系统应用程序是否也需要root才能切换飞行模式?要在Android根设备(手机、平板电脑、笔记本电脑)上打开和关闭飞机/飞行模式,可以执行以下操作: private final String COMMAND_FLIGHT_MODE_1 = "settings put g

从Android 4.2开始,使用普通API不支持打开/关闭飞行模式

当授予WRITE_SECURE_SETTINGS权限时,它可能会工作,但这仅适用于系统应用程序(如我所读)

要在具有root的设备上执行此操作,应执行哪些操作


系统应用程序是否也需要root才能切换飞行模式?

要在Android根设备(手机、平板电脑、笔记本电脑)上打开和关闭飞机/飞行模式,可以执行以下操作:

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);
    }
@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;
}
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());
        }   
    }
}
要检查飞机/飞行模式是否已打开和关闭,请执行以下操作:

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);
    }
@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;
}
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());
        }   
    }
}
要执行
su
命令,请执行以下操作:

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);
    }
@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;
}
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());
        }   
    }
}

由于
设置.Global
中定义的任何内容都可以被系统应用程序读写-甚至是作为系统应用程序创建的第三方应用程序。

请参阅下面的我的答案。可能重复有趣的内容,谢谢。为什么使用“授权”来获得许可不起作用?它是不能授予的权限之一吗?此外,这是否是一个可能在较新的android版本上不起作用的解决办法?从android 4.2及更高版本开始,谷歌已经限制了只对系统应用程序可用的权限,因此“授权”将不起作用。我已经尝试过这条路,但没有成功。我发布的代码适用于大多数安卓4.3和4.4根设备——因为我没有所有安卓物理设备运行4.2+固件的变体——我不能说它适用于所有4.2+根设备。但是,到目前为止,我的用户都没有报告在根4.2+设备上打开飞机/飞行模式时出现任何错误。所以,我想没有消息就是好消息!;)我懂了。它在安卓L上工作吗?你是如何得到这个解决方案的?还有,你的应用程序是什么?我的是这个(我不打算添加这个功能,只是很有趣):所以你发现的更像是一个漏洞,不是吗?它可能迟早会被“修复”,然后你需要找到其他东西,对吗?顺便说一句,我现在已经安装了你的应用程序,我认为你应该将targetSdk设置为尽可能高的值(如文档所示,Lint和google所建议的),另外我认为你应该更改通知,像Widgetsoid一样在其中包含按钮,并允许选择要使用的切换。此外,我也找不到关闭该应用程序的方法……因为谷歌在过去四个固件版本(安卓4.2到安卓L)中没有实施“修复”——我想没有。您提到的功能是我考虑为我的应用程序的未来发布。要关闭应用程序,您只需使用设备上的“后退”按钮,因为我没有按照Hackbod(Android框架工程师)和其他高级Android开发人员的建议在我的应用程序中实现退出选项,他们在这里发表了评论。