android开关飞行模式的方法

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.


你可能感兴趣的:(Android)