Android飞行模式的打开与关闭

在Android4.2以前可以做到使用代码进行飞行模式的开关,步骤如下:

1、首先需要在AndroidManifest.xml中加入权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

2、java代码中首先进行一个putString操作,然后发送一个广播,注意不发送广播是不会生效的:

if (Settings.System.getString(mContentResolver,
					Settings.System.AIRPLANE_MODE_ON).equals("0")) {
	//0表示飞行模式已关闭,1表示飞行模式已打开			
        Settings.System.putString(mContentResolver,
						Settings.System.AIRPLANE_MODE_ON, "1");
        sendBroadcast(intent);
} else {
	Settings.System.putString(mContentResolver,
						Settings.System.AIRPLANE_MODE_ON, "0");
	sendBroadcast(intent);
}

对于Android4.2以后的版本,只能说没办法。

http://developer.android.com/about/versions/android-4.2.html

Some device settings defined by Settings.System are now read-only. If your app attempts to write changes to settings defined inSettings.System that have moved to Settings.Global, the write operation will silently fail when running on Android 4.2 and higher.

Even if your value for android:targetSdkVersion and android:minSdkVersion is lower than 17, your app is not able to modify the settings that havemoved toSettings.Global when running on Android 4.2 and higher.

有个方法可以试试:http://lufengdie.iteye.com/blog/918975


你可能感兴趣的:(android,飞行模式,airplanemode)