我不希望我的用户尝试下载任何东西,除非他们连接了Wi-Fi。然而,我似乎只能判断是否启用了Wi-Fi,但他们仍然可能有3G连接。

android.net.wifi.WifiManager m = (WifiManager) getSystemService(WIFI_SERVICE);
android.net.wifi.SupplicantState s = m.getConnectionInfo().getSupplicantState();
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(s);
if (state != NetworkInfo.DetailedState.CONNECTED) {
    return false;
}

然而,这种状态并不是我所期望的。即使Wi-Fi是连接的,我得到OBTAINING_IPADDR作为状态。


当前回答

我简单地使用以下方法:

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.

其他回答

我简单地使用以下方法:

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.

这是一个更简单的解决方案。参见Stack Overflow 在Android上检查是否启用Wi-Fi。

注:不要忘记将代码添加到manifest.xml文件中以允许权限。如下图所示。

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>

从API级别29开始,NetworkInfo类以及相关的访问方法(如connectivitymanager# getNetworkInfo()和connectivitymanager# getActiveNetworkInfo())都已弃用。

文档现在建议人们使用ConnectivityManager。NetworkCallback API用于异步回调监控,或使用connectivitymanager# getNetworkCapabilities或connectivitymanager# getLinkProperties来同步访问网络信息

调用者应该使用ConnectivityManager。NetworkCallback API来了解连接变化,或切换到使用connectivitymanager# getNetworkCapabilities或connectivitymanager# getLinkProperties来同步获取信息。


要检查是否连接了WiFi,下面是我使用的代码:

科特林:

val connMgr = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
connMgr?: return false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    val network: Network = connMgr.activeNetwork ?: return false
    val capabilities = connMgr.getNetworkCapabilities(network)
    return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
} else {
    val networkInfo = connMgr.activeNetworkInfo ?: return false
    return networkInfo.isConnected && networkInfo.type == ConnectivityManager.TYPE_WIFI
}

Java:

ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connMgr == null) {
    return false;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    Network network = connMgr.getActiveNetwork();
    if (network == null) return false;
    NetworkCapabilities capabilities = connMgr.getNetworkCapabilities(network);
    return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
} else {
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}

记住还要向Manifest文件添加ACCESS_NETWORK_STATE权限。

添加到JAVA:

public boolean CheckWifiConnection() {
        ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
        if (conMgr.getActiveNetworkInfo() != null
                && conMgr.getActiveNetworkInfo().isAvailable()
                && conMgr.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false;
        }
    }

在Manifest文件中添加以下权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

使用WifiManager你可以做到:

WifiManager wifi = (WifiManager) getSystemService (Context.WIFI_SERVICE);
if (wifi.getConnectionInfo().getNetworkId() != -1) {/* connected */}

方法getNeworkId只有在没有连接到网络时才返回-1;