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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值