这里是一款采用Android 4.0的平板电脑。
wifiManager.isWifiEnabled()
返回true
但如果我查看Android默认的Wifi设置,Wifi仍然关闭...
如何在不使用isWifiEnabled()
的情况下检查WiFi是否已开启?
答案 0 :(得分:5)
您可以尝试使用getWifiState()
获取启用Wi-Fi的状态
返回:
其中一个WIFI_STATE_DISABLED
,WIFI_STATE_DISABLING
,WIFI_STATE_ENABLED
,WIFI_STATE_ENABLING
,WIFI_STATE_UNKNOWN
答案 1 :(得分:2)
尝试以下代码。
SupplicantState supState;
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
在您调用getSupplicantState()时会返回其中一种状态;
ASSOCIATED - Association completed.
ASSOCIATING - Trying to associate with an access point.
COMPLETED - All authentication completed.
DISCONNECTED - This state indicates that client is not associated, but is likely to start looking for an access point.
DORMANT - An Android-added state that is reported when a client issues an explicit DISCONNECT command.
FOUR_WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress.
GROUP_HANDSHAKE - WPA Group Key Handshake in progress.
INACTIVE - Inactive state.
INVALID - A pseudo-state that should normally never be seen. SCANNING - Scanning for a network.
UNINITIALIZED - No connection.
答案 2 :(得分:1)
您可以使用Settings.Global.WIFI_ON
public boolean isWiFiSettingOn() {
boolean isOn = false;
try {
isOn = Settings.Global.getInt(mContentResolver, Settings.Global.WIFI_ON) != 0;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
// This should never happen
Log.e(TAG, "Couldn't retrieve status of Settings.Global.WIFI_ON - returning the default value FALSE")`enter code here`;
}
Log.d(TAG, "isWiFiSettingOn? - " + isOn);
return isOn;
}
答案 3 :(得分:0)
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
并检查wifi.states ....
答案 4 :(得分:0)
您还必须检查您是否拥有AndroidManifest.xml中的权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />