[Android Pro] 监听WIFI 打开广播

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
        IntentFilter filter = new IntentFilter();
        filter.setPriority(2147483647);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        registerReceiver(mWifiReceiver, filter);
private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 这个监听wifi的打开与关闭,与wifi的连接无关
                    abortBroadcast();
                    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
                    if (FeatureConfig.DEBUG_LOG) {
                        Log.e("H3c", "wifiState : " + wifiState);
                    }
                    switch (wifiState) {
                        case WifiManager.WIFI_STATE_DISABLED:
                            if (FeatureConfig.DEBUG_LOG) {
                                Log.e("H3c", "wifiState WIFI_STATE_DISABLED ");
                            }
                            break;
                        case WifiManager.WIFI_STATE_ENABLED:
                            abortBroadcast();
                            if (FeatureConfig.DEBUG_LOG) {
                                Log.e("H3c", "wifiState WIFI_STATE_ENABLED ");
                            }
                            if (FeatureConfig.DEBUG_LOG) {
                                Log.e("H3c", "validatePackageNames contains : " + validatePackageNames.contains(WifiManager.WIFI_STATE_CHANGED_ACTION));
                            }
                            // 1: disable wifi
WifiManager wifiManager = (WifiManager) DuAppLockService.this.getSystemService(Context.WIFI_SERVICE);
boolean result = wifiManager.setWifiEnabled(false);
break; } } } } };

open wifi :

if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(validatePkg)) {
     // enable wifi
    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(true);
}

 

posted @ 2015-09-23 10:30  demoblog  阅读(3364)  评论(0编辑  收藏  举报