不废话,直接上代码:
// 打开或者关闭飞行模式,0是关闭,1是开启。
public static void toggleAirPlaneMode(Context context) {
Log.d("slh", "toggleAirPlaneMode() enter.");
if (null == context) {
Log.e("slh", "toggleAirPlaneMode() context is null.");
return;
}
Context appContext = context.getApplicationContext();
boolean isEnabled = Settings.Global.getInt(appContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
Log.d("slh", "toggleAirPlaneMode() isEnabled = " + isEnabled);
Settings.Global.putInt(appContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
Intent airIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
airIntent.putExtra("state", !isEnabled);
appContext.sendBroadcast(airIntent);
}
这里需要注意的是该方法只试用系统应用,而且老的API:android.provider.Settings.System已经废弃,新的API为:android.provider.Settings.Global