如何在新的安卓版本中开启/关闭飞行模式(即使已获取root权限)?

如何在新的安卓版本中开启/关闭飞行模式(即使已获取root权限)?

如果您的安卓设备(手机、平板电脑或笔记本)已获取 root权限,您可以按照以下步骤打开或关闭飞行模式:

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;

}

执行su命令,请按照以下步骤操作:

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

}

}

}

或者,如果您的应用程序:

使用Android框架的证书进行签名;并且

已安装到 /system/app/ 目录中;并且

在 AndroidManifest.xml 文件中声明了相关标记(例如 WRITE_SECURE_SETTINGS 等)。

那么,您只需执行以下操作:

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

由于在Settings.Global中定义的任何内容都可以被系统应用程序读写——即使是作为系统应用程序创建的第三方应用程序。

相关推荐

京东热销排行榜哪里看?商品上榜条件是啥?
365体育竞彩足球

京东热销排行榜哪里看?商品上榜条件是啥?

📅 06-27 👁️ 9427
一起盘点下做问答都有哪些平台!你还知道哪些?
365bet体育在线赌博

一起盘点下做问答都有哪些平台!你还知道哪些?

📅 07-09 👁️ 8720
奶粉代理商怎样做(奶粉总代理怎么申请)
bat365官方登录中文

奶粉代理商怎样做(奶粉总代理怎么申请)

📅 06-30 👁️ 590