您的位置:首页 > 移动开发 > Android开发

android开关飞行模式的方法

2014-01-17 14:00 676 查看
android中很多场景需要判断和设置飞行模式


1.判断当前飞行模式状态:直接从Settings.Global数据库中读取飞行模式当前状态值

Settings.Global.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, STATE_DISABLED);


2.设置飞行模式状态的方法如下:

首先将飞行模式新状态写入数据库,然后发出飞行模式改变的广播;

Settings.Global.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, airplaneMode);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", STATE_ENABLED == airplaneMode);
context.sendBroadcast(intent);




注:android4.1中飞行模式状态值保存在Settings.system数据库表中,但android4.2已经将该值移到Settings.Global数据库表中。android4.2上切不可使用Settings.system,不然会提示无效:



01-17 13:45:00.150: W/Settings(2972): Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: