在這裏,Android 4.0平板電腦。Android:wifiManager.isWifiEnabled()返回TRUE,但WiFi被禁用
wifiManager.isWifiEnabled()
回報true
但如果我看的Android默認WLAN設置,無線網絡仍然是關...
我如何檢查無線網絡連接,而不使用isWifiEnabled()
?
在這裏,Android 4.0平板電腦。Android:wifiManager.isWifiEnabled()返回TRUE,但WiFi被禁用
wifiManager.isWifiEnabled()
回報true
但如果我看的Android默認WLAN設置,無線網絡仍然是關...
我如何檢查無線網絡連接,而不使用isWifiEnabled()
?
你可以嘗試使用getWifiState()
獲取Wi-Fi功能的狀態。
返回:
一個WIFI_STATE_DISABLED
,WIFI_STATE_DISABLING
,WIFI_STATE_ENABLED
,WIFI_STATE_ENABLING
,WIFI_STATE_UNKNOWN
ok,getWiFiState()== WIFI_STATE_DISABLED並且WiFiEnabled()返回true ... –
Intresting情況,我會嘗試去處理它。我寫信給你,如果我找到一些答案。 – Arseniy
好的,讓我知道。謝謝! –
嘗試以下代碼。
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.
其實supState是UNINITIALIZED和isWifiEnabled()= TRUE ...是否正確? –
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
並檢查wifi.states ....
你還必須檢查是否有針對該許可您的AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
您可以使用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;
}
有什麼好笑的是,isWifiEnabled返回'getWifiState()== WIFI_STATE_ENABLED;' – njzk2
如果您正在測試的WiFi改變其狀態之後,再isWifiEnabled()的結果是錯誤的。我遇到了這個問題,試着暫停一下,然後測試一下,然後運行。說,5秒後。重點是傳播需要幾秒鐘的時間。 –