2

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!

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
Shawn Henderson
  • 83
  • 1
  • 1
  • 9

3 Answers3

9

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.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Why would this make any difference. Presumably the OP is calling `getSystemService(...)` from within an `Activity` or other application component which extends `Context` otherwise the code would fail to even compile. This is why the downvote. – Squonk Oct 05 '12 at 19:26
  • Uh, I'm not saying using `mContext` will solve the problem. I'm using a different return statement. I've used this code numerous times without fail, and hence suggested it. – Raghav Sood Oct 05 '12 at 19:27
  • But the OP wants to know why `isConnected()` ALWAYS returns true. Your suggestion of `isConnectedOrConnecting()` isn't going to change anything. – Squonk Oct 05 '12 at 19:30
  • Maybe I am doing this in the wrong order or something. I have now put the above code in. Then in onCreate() I have a line System.out.Printlin("test :" + isOnline()); have the computer wifi turned off and i still get true. – Shawn Henderson Oct 05 '12 at 19:46
  • Are you using an Emulator or a device? – Raghav Sood Oct 05 '12 at 19:47
  • Is this because im using an emulator? – Shawn Henderson Oct 05 '12 at 19:49
  • 1
    @ShawnHenderson : Yes. Turning off the computer's network won't affect the emulator because it will still be able to connect to the computer's `localhost` port even if it can't connect to the outside world. – Squonk Oct 05 '12 at 19:54
  • 1
    If you're using an emulator, then you have to go into the settings and disable wifi and mobile data. Otherwise it won't work properly. – Raghav Sood Oct 05 '12 at 19:54
  • Do not forget to add in manifest.xml – denispyr Apr 28 '15 at 20:57
4

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;
        }
    } 
slezadav
  • 6,104
  • 7
  • 40
  • 61
1
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" />
jose920405
  • 7,982
  • 6
  • 45
  • 71