WiFi的打开与关闭
WiFiManager状态常数及意义
WifiManager状态常数 | 意义 |
WifiManager.WIFI_STATE_ENABLED | wifi已经打开 |
WifiManager.WIFI_STATE_ENABLING | wifi打开中 |
WifiManager.WIFI_STATE_DISABLING | wifi关闭中 |
WifiManager.WIFI_STATE_DISABLED | wifi已经关闭 |
WifiManager.WIFI_STATE_UNKNOWN | 未知的Wifi状态 |
通过WiFiManager打开WiFi,mWiFiManager.setWifiEnabled(true)的状态变化过程:
1、WifiManager.WIFI_STATE_DISABLED
2、WifiManager.WIFI_STATE_ENABLING
3、WifiManager.WIFI_STATE_ENABLED
通过WiFiManager关闭WiFi,mWiFiManager.setWifiEnabled(false)的状态变化过程:
1、WifiManager.WIFI_STATE_ENABLED
2、WifiManager.WIFI_STATE_DISABLING
3、WifiManager.WIFI_STATE_DISABLED
如果状态处于WIFI_STATE_ENABLING时,运行一次“打开”功能,将导致错误,因此,程序必须对这类的事件状态进行捕捉,否则,就不知道WiFi究竟何时可以打开完毕,而手机与WiFi AP的连接也有访问的时间,并非是马上单击就能立即反应其打开结果。
必须加权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
代码如下:
cb.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (cb.isChecked()) { // 尝试打开wifi服务 try { if (!wifiManager.isWifiEnabled() && wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING) { // wifi没有打开,并且没有处于打开过程中 if (wifiManager.setWifiEnabled(true)) { tv.setText(getResources().getString( R.string.str_start_wifi_done)); cb.setText(getResources().getString( R.string.str_checked)); } else { tv.setText(getResources().getString( R.string.str_start_wifi_failed)); cb.setText(getResources().getString( R.string.str_uncheck)); cb.setChecked(false); } } else { int strId; switch (wifiManager.getWifiState()) { case WifiManager.WIFI_STATE_ENABLING: // wifi正在打开中... strId = R.string.str_wifi_enabling; break; case WifiManager.WIFI_STATE_DISABLING: // wifi正在关闭中... strId = R.string.str_wifi_disabling; break; case WifiManager.WIFI_STATE_ENABLED: // wifi已经关闭 strId = R.string.str_wifi_enabled; break; case WifiManager.WIFI_STATE_UNKNOWN: // wifi状态没有 default: strId = R.string.str_wifi_unknow; break; } tv.setText(getResources().getString( R.string.str_start_wifi_failed) + "::" + getResources().getString(strId)); cb.setText(getResources().getString( R.string.str_checked)); cb.setChecked(false); } } catch (Exception e) { Log.e("zyh", e.toString()); } } else { // 尝试关闭wifi服务 try { // 判断wifi状态是否已经启动 if (wifiManager.isWifiEnabled()) { // 关闭wifi if (wifiManager.setWifiEnabled(false)) { tv.setText(getResources().getString( R.string.str_stop_wifi_done)); cb.setText(getResources().getString( R.string.str_uncheck)); } else { tv.setText(getResources().getString( R.string.str_stop_wifi_failed)); cb.setText(getResources().getString( R.string.str_checked)); cb.setChecked(true); } } else { int strId; switch (wifiManager.getWifiState()) { case WifiManager.WIFI_STATE_ENABLING: // wifi正在打开中... strId = R.string.str_wifi_enabling; break; case WifiManager.WIFI_STATE_DISABLING: // wifi正在关闭中... strId = R.string.str_wifi_disabling; break; case WifiManager.WIFI_STATE_DISABLED: // wifi已经关闭 strId = R.string.str_wifi_disabled; break; case WifiManager.WIFI_STATE_UNKNOWN: // wifi状态没有 default: strId = R.string.str_wifi_unknow; break; } tv.setText(getResources().getString( R.string.str_stop_wifi_failed) + "::" + getResources().getString(strId)); cb.setText(getResources().getString( R.string.str_uncheck)); cb.setChecked(true); } } catch (Exception e) { Log.e("zyh", e.toString()); } } } });
扫描附件可用WiFi:
// 设置WifiConfiguration对象 List<WifiConfiguration> configuredNetworks = wifiManager .getConfiguredNetworks(); if (configuredNetworks != null && configuredNetworks.size() > 0) { ((ListView) findViewById(R.id.lv)) .setAdapter(new MyAdapter(MainActivity.this, configuredNetworks)); } else { Toast.makeText(MainActivity.this, "没有可用wifi", 0).show(); }