android中监听wifi的状态和判断是否开启相关操作

1、使用APP打开系统的设置界面或者WiFi连接界面

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
 //直接进入手机中的wifi网络设置界面
            }
startActivity(new Intent(Settings.ACTION_SETTINGS)); //直接进入手机中设置界面
            }

2、监听wifi状态代码如下: 1)注册广播:

IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(new WifiReceiver(), filter);
    //监听类
   public class WifiReceiver extends BroadcastReceiver {
        private static final String TAG = "wifiReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {
                Log.i(TAG, "wifi信号强度变化");
            }
            //wifi连接上与否
            if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

                NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
                    Log.i(TAG, "wifi断开");
                    setWifIcon(false);

                } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
                    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    //获取当前wifi名称
                    Log.i(TAG, "连接到网络 " + wifiInfo.getSSID());
                    setWifIcon(true);
                }
            }
            //wifi打开与否
            if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
                if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
                    Log.i(TAG, "系统关闭wifi");
                    setWifIcon(false);
                } else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
                    Log.i(TAG, "系统开启wifi");
                    setWifIcon(true);
                }
            }
        }
    }
//设置wifi图标
    void setWifIcon(final boolean isOpen) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isOpen) {
                    icon_wifi.setImageResource(R.mipmap.icon_wifi);
                } else {
                    icon_wifi.setImageResource(R.mipmap.icon_wifi_no);
                }
            }
        });
    }