1

在这里,一台装有 Android 4.0 的平板电脑。

wifiManager.isWifiEnabled()

返回true,但如果我查看 Android 默认 Wifi 设置,Wifi 仍处于关闭状态...

如何在不使用的情况下检查 WiFi 是否打开isWifiEnabled()

4

5 回答 5

5

您可以尝试使用getWifiState()
获取 Wi-Fi 启用状态。
返回:, , , ,
之一WIFI_STATE_DISABLEDWIFI_STATE_DISABLINGWIFI_STATE_ENABLEDWIFI_STATE_ENABLINGWIFI_STATE_UNKNOWN

于 2012-09-24T07:13:51.670 回答
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.
于 2012-09-24T07:12:57.067 回答
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;
    }
于 2015-01-16T13:02:36.147 回答
0

您还必须在 AndroidManifest.xml 中检查您是否拥有该权限

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
于 2013-07-31T19:03:16.783 回答
0
    ConnectivityManager cm =    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

并检查 wifi.states....

于 2012-09-24T07:16:39.420 回答