在这里,一台装有 Android 4.0 的平板电脑。
wifiManager.isWifiEnabled()
返回true
,但如果我查看 Android 默认 Wifi 设置,Wifi 仍处于关闭状态...
如何在不使用的情况下检查 WiFi 是否打开isWifiEnabled()
?
您可以尝试使用getWifiState()
获取 Wi-Fi 启用状态。
返回:, , , ,
之一WIFI_STATE_DISABLED
WIFI_STATE_DISABLING
WIFI_STATE_ENABLED
WIFI_STATE_ENABLING
WIFI_STATE_UNKNOWN
试试下面的代码。
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.
您可以使用 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;
}
您还必须在 AndroidManifest.xml 中检查您是否拥有该权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
并检查 wifi.states....