NetworkInfo.isConnected() 和 NetworkInfo.getDetailedState() 之间的区别 == NetworkInfo.DetailedState.CONNECTED。用什么?

Difference between NetworkInfo.isConnected() and NetworkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED. What to use?

我想知道哪种方法可以准确的检测网络状态,从而连接成功。

Java:

 ConnectivityManager manager =
               (ConnectivityManager)getApplication.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info=manager.getActiveNetworkInfo()

if(info!=null && info.isConnected()){
// you are online for sure.
}

科特林:

val manager = application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        val info = manager.getActiveNetworkInfo()

        if (info != null && info.isConnected()) {
            // you are online for sure.
        }

我在开发中使用上面的代码来确保我的设备已连接到互联网。

阅读此 thread 了解区别以及如何更改状态

来自源代码

public boolean isConnected() {
    synchronized (this) {
        return mState == State.CONNECTED;
    }
}

原来是一样的

从这里开始https://developer.android.com/training/monitoring-device-state/connectivity-monitoring#java

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnected();

您需要将其添加到清单中

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

因此,如果我们查看 NetworkInfo.java class 的 source code,您会看到网络详细状态声明为 Enum,

public enum DetailedState {
        /** Ready to start data connection setup. */
        IDLE,
        /** Searching for an available access point. */
        SCANNING,
        /** Currently setting up data connection. */
        CONNECTING,
        /** Network link established, performing authentication. */
        AUTHENTICATING,
        /** Awaiting response from DHCP server in order to assign IP address information. */
        OBTAINING_IPADDR,
        /** IP traffic should be available. */
        CONNECTED,
        /** IP traffic is suspended */
        SUSPENDED,
        /** Currently tearing down data connection. */
        DISCONNECTING,
        /** IP traffic not available. */
        DISCONNECTED,
        /** Attempt to connect failed. */
        FAILED,
        /** Access to this network is blocked. */
        BLOCKED,
        /** Link has poor connectivity. */
        VERIFYING_POOR_LINK,
        /** Checking if network is a captive portal */
        CAPTIVE_PORTAL_CHECK
    }

但是如果您阅读了这些 DetailedState 的评论,它会在下面说明这些

The fine-grained state of a network connection. This level of detail is probably of interest to few applications. Most should use android.net.NetworkInfo.State State instead.

NetworkInfo.java class 中的 isConnected() 方法仅检查 State.CONNECTED State

public boolean isConnected() {
        synchronized (this) {
            return mState == State.CONNECTED;
        }
    }

基本上如果你使用

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

这应该足够了,因为上面的代码将查询活动网络并确定它是否具有 Internet 连接。一旦你知道了,你就可以继续访问互联网资源了。