android 飞行模式 注册广播后 三种状态监听

来源:互联网 发布:小学教师网络研修计划 编辑:程序博客网 时间:2024/05/16 17:00
[html] view plaincopy
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.     private static final String TAG = "MainActivity";  
  3.     private int airState;  
  4.     private Button startButton;  
  5.     private Button closeButton;  
  6.     private Button rButton;  
  7.     boolean state = false;  
  8.     IntentFilter airIntentFilter;  
  9.   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         airIntentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");  
  15.         registerReceiver(airReceiver, airIntentFilter);  
  16.         state = true;  
  17.   
  18.         startButton = (Button) findViewById(R.id.main_send_start);  
  19.         closeButton = (Button) findViewById(R.id.main_send_close);  
  20.         rButton = (Button) findViewById(R.id.main_receiver);  
  21.         startButton.setOnClickListener(this);  
  22.         closeButton.setOnClickListener(this);  
  23.         rButton.setOnClickListener(this);  
  24.     }  
  25.   
  26.     /**  
  27.      * 打开飞行模式  
  28.      *   
  29.      * @throws InterruptedException  
  30.      */  
  31.     private void satrtAirplane() {  
  32.         /*  
  33.          * ContentResolver cr = getContentResolver(); if  
  34.          * (android.provider.Settings.System.getString(cr,  
  35.          * Settings.System.AIRPLANE_MODE_ON).equals("0")) {  
  36.          * 获取当前飞行模式状态,返回的是String值0,或1. 0为关闭飞行, 1为开启飞行  
  37.          */  
  38.         Log.i(TAG, "开启飞行");  
  39.         Settings.System.putString(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, "1");  
  40.         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);  
  41.         sendBroadcast(intent);  
  42.         Log.i(TAG, "开启飞行执行完毕");  
  43.     }  
  44.   
  45.     /**  
  46.      * 关闭飞行模式  
  47.      */  
  48.     public void closeAirplane() {  
  49.         Log.i(TAG, "关闭飞行模式方法 ");  
  50.         Settings.System.putString(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, "0");  
  51.         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);  
  52.         sendBroadcast(intent);  
  53.         Log.i(TAG, "关闭飞行模式方法 执行完毕");  
  54.     }  
  55.   
  56.     BroadcastReceiver airReceiver = new BroadcastReceiver() {  
  57.         @Override  
  58.         public void onReceive(Context context, Intent intent) {  
  59.             Bundle bundle = intent.getExtras();  
  60.             if (bundle != null) {  
  61.                 airState = bundle.getInt("state");  
  62.   
  63.                 Log.e(TAG, "飞行模式状态 1为开启状态,0为关闭状态 airState==" + airState);  
  64.                 switch (airState) {  
  65.                 case 0: // 飞行模式关闭成功状态  
  66.                     Toast.makeText(getApplicationContext(), "关闭状态airState=" + airState, 0).show();  
  67.                     Log.i(TAG, "关闭状态  airState=" + airState);  
  68.                     break;  
  69.                 case 1: // 飞行模式 关闭过程  
  70.                     Toast.makeText(getApplicationContext(), "开启状态airState=" + airState, 0).show();  
  71.                     Log.i(TAG, "开启状态 airState=" + airState);  
  72.                     break;  
  73.                 case 3: // 飞行模式开启成功  
  74.                     Toast.makeText(getApplicationContext(), "开启成功状态airState=" + airState, 0).show();  
  75.                     Log.i(TAG, "飞行模式状态开启成功  airState=" + airState);  
  76.                     break;  
[html] view plaincopy
  1.         }  
  2.        }  
  3.   }  
  4.  };                       
  5.     @Override  
  6.     public void onClick(View v) {  
  7.         // TODO  
  8.         switch (v.getId()) {  
  9.         case R.id.main_send_start:  
  10.             satrtAirplane();  
  11.             break;  
  12.         case R.id.main_send_close:  
  13.             closeAirplane();  
  14.             break;  
  15.         case R.id.main_receiver:  
  16.             if (state) {  
  17.                 unregisterReceiver(airReceiver);  
  18.                 state = false;  
  19.             } else {  
  20.                 registerReceiver(airReceiver, airIntentFilter);  
  21.                 state = true;  
  22.             }  
  23.             break;  
  24.         default:  
  25.             break;  
  26.         }  
  27.   
  28.     }  
  29. }  

个人飞行模式小记:

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

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

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

0 0
原创粉丝点击