Android 监听wifi热点打开和关闭广播

wifi热点的打开和关闭也是有广播的,监听android.net.wifi.WIFI_AP_STATE_CHANGED广播就可以了,记录下做个笔记

  private BroadcastReceiver wifiApBroadcast = new  BroadcastReceiver(){

      @Override
      public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if("android.net.wifi.WIFI_AP_STATE_CHANGED".equals(action)){
            //便携式热点的状态为:10---正在关闭;11---已关闭;12---正在开启;13---已开启
            int state = intent.getIntExtra("wifi_state",  0);
            Log.e("MainActivity","热点开关状态:state= "+String.valueOf(state));
            if(state == 13){
                Log.e("MainActivity","热点已开启");
            }else if(state == 11){
                Log.e("MainActivity","热点已关闭");
            }else if(state == 10){
                Log.e("MainActivity","热点正在关闭");
            }else if(state == 12){
                Log.e("MainActivity","热点正在开启");
            }
         }
      }
   };

注册广播

IntentFilter intentFilter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");
mContext.registerReceiver(wifiApBroadcast,intentFilter);

你可能感兴趣的:(android)