我不希望我的用户尝试下载任何东西,除非他们连接了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作为状态。


当前回答

在新版Android中

private void getWifiInfo(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Network[] networks = connManager.getAllNetworks();

    if(networks == null || networks.length == 0)
        return;

    for( int i = 0; i < networks.length; i++) {
        Network ntk = networks[i];
        NetworkInfo ntkInfo = connManager.getNetworkInfo(ntk);
        if (ntkInfo.getType() == ConnectivityManager.TYPE_WIFI && ntkInfo.isConnected() ) {
            final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            if (connectionInfo != null) {
                // add some code here
            }
        }

    }
}

还要加上前提

其他回答

val wifi = context!!.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?        
         if (wifi!!.isWifiEnabled) 
              //do action here
    
         else 
             //do action here
    
        
                    

您应该能够使用ConnectivityManager来获取Wi-Fi适配器的状态。从那里你可以检查它是否连接,甚至可用。

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}

注意:应该注意(对于这里的n00bies),您需要添加

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

到你的

AndroidManifest.xml来工作。

注2:公共NetworkInfo getNetworkInfo (int networkType)现在已弃用:

此方法在API级别23中已弃用。这个方法没有 支持多个相同类型的网络连接。使用 getAllNetworks()和getNetworkInfo(android.net.Network)代替。

注3:public static final int TYPE_WIFI现在已弃用:

此常量在API级别28中已弃用。 应用程序应该使用networkcapabilities . hatransporsport (int)或requestNetwork(NetworkRequest, NetworkCallback)来请求一个合适的网络。对于支持的传输。

ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
boolean is3g = manager.getNetworkInfo(
                  ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
boolean isWifi = manager.getNetworkInfo(
                    ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

Log.v("", is3g + " ConnectivityManager Test " + isWifi);
if (!is3g && !isWifi) {
    Toast.makeText(
        getApplicationContext(),
        "Please make sure, your network connection is ON ",
        Toast.LENGTH_LONG).show();
}
else {
    // Put your function() to go further;
}

试试这个方法。

public boolean isInternetConnected() {
    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean ret = true;
    if (conMgr != null) {
        NetworkInfo i = conMgr.getActiveNetworkInfo();

        if (i != null) {
            if (!i.isConnected()) {
                ret = false;
            }

            if (!i.isAvailable()) {
                ret = false;
            }
        }

        if (i == null)
            ret = false;
    } else
        ret = false;
    return ret;
}

这种方法将有助于找到互联网连接是否可用。

我简单地使用以下方法:

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.