Shubham Dadhich
Shubham Dadhich

Reputation: 147

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

I want to know which method will be precise to check the network state for successfully getting connected.

Upvotes: 5

Views: 932

Answers (3)

AADProgramming
AADProgramming

Reputation: 6345

So if we look at the source code of NetworkInfo.java class you will see that the network detailed states are declared as 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
    }

But if you read the comments for these DetailedState it says below about these

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.

The isConnected() method inside the NetworkInfo.java class is checking against the State.CONNECTED State only,

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

Essentially if you use

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

that should suffice as above code will query the active network and determine if it has Internet connectivity. Once you know it, you can proceed with accessing internet resource.

Upvotes: 3

svkaka
svkaka

Reputation: 4022

from source code

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

so it's the same

From here 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();

you will need to add this to your manifest

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

Upvotes: 0

Anmol
Anmol

Reputation: 8670

Java:

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

NetworkInfo info=manager.getActiveNetworkInfo()

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

Kotlin:

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

        val info = manager.getActiveNetworkInfo()

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

I use Above code in my development to be sure that my device is connected to internet.

Read this thread to know the difference and how state can be changed

Upvotes: 1

Related Questions