Shawn Henderson
Shawn Henderson

Reputation: 83

Check online status android

Im using this code to check if the device is online as the app loads.

public boolean isOnline() 
{
    ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected());
}  

But this ALWAYS returns true, even if it have the computer Wifi turned off for testing. Is this function just testing for the ability to connect or actual connection?

Thanks!

Upvotes: 2

Views: 11378

Answers (3)

jose920405
jose920405

Reputation: 8049

protected boolean isOnline() 
{
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            if (activeNetwork.isConnected())
                haveConnectedWifi = true;
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            if (activeNetwork.isConnected())
                haveConnectedMobile = true;
        }
    }

    return haveConnectedWifi || haveConnectedMobile;
}

And needs this 2 permissions in manifest

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

Upvotes: 1

Raghav Sood
Raghav Sood

Reputation: 82553

Try using:

private boolean isOnline()
    {
        try
        {
            ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            return cm.getActiveNetworkInfo().isConnectedOrConnecting(); 
        }
        catch (Exception e)
        {
            return false;
        }
    }

Where mContext is the context you're using.

My guess is that your code would work fine on a device, but you may be testing on an emulator. I've noticed that sometimes the emulator stays connected even when the computer's internet if switched off. To achieve correct functionality, you should go into the settings and disable WiFi and Mobile data from there, instead of turning the computer WiFi off.

In addition, the code I gave above would also return true if the device is in the process of connecting, while the one you were using would only return true if you already had an established connection.

Upvotes: 9

slezadav
slezadav

Reputation: 6141

This code has always worked for me.

`

public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

// test for connection
        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false;
        }
    } 

Upvotes: 4

Related Questions