我计划挂断电话,我发现这是其中一种解决方法。如何通过代码激活飞行模式?
这样,我将根据某个事件挂断电话。
我计划挂断电话,我发现这是其中一种解决方法。如何通过代码激活飞行模式?
这样,我将根据某个事件挂断电话。
请参考博客文章Android: Controlling Airplane Mode,
仅适用于API 16及以下版本。
// Toggle airplane mode.
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
// Post an intent to reload.
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
其中isEnabled
表示飞行模式是否启用。
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
才能使此功能正常工作。 - Tommiehttp://developer.android.com/reference/android/provider/Settings.Global.html#AIRPLANE_MODE_ON
int mode = isEnable ? 1 : 0;
Runtime.getRuntime().exec("settings put global airplane_mode_on "+mode);
SystemClock.sleep(2000);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", isEnable);
this.sendBroadcast(intent);
shell uid 可以更改 airplane_mode_on,但发送 ACTION_AIRPLANE_MODE_CHANGED 需要权限(需要系统 uid)
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
public static boolean getAirplaneMode(Context context) {
try {
int airplaneModeSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
return airplaneModeSetting==1?true:false;
} catch (SettingNotFoundException e) {
return false;
}
}