, 당신은 다음을 수행 할 수
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);
}
은 확인하려면를 비행기/비행 모드 여부 ON과 OFF 이미 다음을 수행하십시오
@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());
}
}
}
또는 앱 경우 :
- 는 안드로이드 프레임 워크의 인증서로 서명되었다;
/system/app/
디렉토리에 설치되었습니다. 및
AndroidManifest.xml
파일에 선언 된 관련 태그가 있어야합니다 (예 : WRITE_SECURE_SETTINGS
등).
다음은이 작업을 수행 할 수 있습니다 Settings.Global
에 정의 아무것도 할 수
Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
때문에 읽기 - 쓰기 시스템 앱 - 시스템 응용 프로그램으로 만들어도 타사 응용 프로그램을.
내 대답은 아래를 참조하십시오. – ChuongPham
[루트를 사용하여 안드로이드 4.2에서 비행기 모드를 토글하는 방법]의 복제본을 만들 수 있습니까? (http://stackoverflow.com/questions/15861046/how-to-toggle-airplane-mode-on-android-4-2-using- 루트) – user1806772