public class MainActivity extends Activity implements OnClickListener {private static final String TAG = "MainActivity";private int airState;private Button startButton;private Button closeButton;private Button rButton;boolean state = false;IntentFilter airIntentFilter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);airIntentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");registerReceiver(airReceiver, airIntentFilter);state = true;startButton = (Button) findViewById(R.id.main_send_start);closeButton = (Button) findViewById(R.id.main_send_close);rButton = (Button) findViewById(R.id.main_receiver);startButton.setOnClickListener(this);closeButton.setOnClickListener(this);rButton.setOnClickListener(this);}/** * 打开飞行模式 *  * @throws InterruptedException */private void satrtAirplane() {/* * ContentResolver cr = getContentResolver(); if * (android.provider.Settings.System.getString(cr, * Settings.System.AIRPLANE_MODE_ON).equals("0")) { * 获取当前飞行模式状态,返回的是String值0,或1. 0为关闭飞行, 1为开启飞行 */Log.i(TAG, "开启飞行");Settings.System.putString(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, "1");Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);sendBroadcast(intent);Log.i(TAG, "开启飞行执行完毕");}/** * 关闭飞行模式 */public void closeAirplane() {Log.i(TAG, "关闭飞行模式方法 ");Settings.System.putString(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, "0");Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);sendBroadcast(intent);Log.i(TAG, "关闭飞行模式方法 执行完毕");}BroadcastReceiver airReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();if (bundle != null) {airState = bundle.getInt("state");Log.e(TAG, "飞行模式状态 1为开启状态,0为关闭状态 airState==" + airState);switch (airState) {case 0: // 飞行模式关闭成功状态Toast.makeText(getApplicationContext(), "关闭状态airState=" + airState, 0).show();Log.i(TAG, "关闭状态  airState=" + airState);break;case 1: // 飞行模式 关闭过程Toast.makeText(getApplicationContext(), "开启状态airState=" + airState, 0).show();Log.i(TAG, "开启状态 airState=" + airState);break;case 3: // 飞行模式开启成功Toast.makeText(getApplicationContext(), "开启成功状态airState=" + airState, 0).show();Log.i(TAG, "飞行模式状态开启成功  airState=" + airState);break;
}}}}; @Overridepublic void onClick(View v) {// TODOswitch (v.getId()) {case R.id.main_send_start:satrtAirplane();break;case R.id.main_send_close:closeAirplane();break;case R.id.main_receiver:if (state) {unregisterReceiver(airReceiver);state = false;} else {registerReceiver(airReceiver, airIntentFilter);state = true;}break;default:break;}}}

个人飞行模式小记:

每次注册飞行模式广播,系统会自动返回当前的 飞行模式状态

当飞行模式未开启:
1点击开启后,会接收到广播 airState=3飞行开启成功
2 点击关闭后,系统无反应
3 点击取消注册广播,广播关闭
当飞行模式开启过程中:
1点击开启,系统无反应,直至开启成功 ,接收到 3
2点击关闭后,系统 关闭飞行模式

当飞行模式开启:
1 点击开启,系统无反应
2 点击关闭后,系统 关闭飞行模式依次 返回 1 0 0 0

当飞行模式关闭过程中:
1点击开启后,会接收到广播 airState=3飞行开启成功
2 点击关闭后,系统 继续关闭过程

更多相关文章

  1. Android之进程查看,关闭(可操作所有进程)
  2. android 的单例模式
  3. Android(安卓)Wear 进阶 2 Creating Wearable Apps-创建手表程序
  4. android 点击屏幕关闭 软键盘
  5. Android(安卓)文件读写的例子
  6. android下TTS的传感器切换听筒和扬声器测试
  7. android 如何判断GPS,NETWORK开启状态
  8. Android(安卓)多级菜单的实现
  9. Android(安卓)实现Activity后台运行

随机推荐

  1. Google Map Android API V2中API Key的生
  2. Android Studio实用插件集合
  3. Android动态设置主题样式
  4. 基于Service与ContentProvider的音乐播放
  5. [Android Studio]Building 'xxx' Gradle
  6. android apk 获取system权限
  7. android左右上下手势判断
  8. 转 Android:文件下载和写入SD卡学习小结
  9. 1.4.2 Android Studio工程目录结构分析
  10. Ubuntu下安装Android Eclipse开发环境