2

is there any simple way, how to check if the device is actively connected into internet (= is connected via GPRS, EDGE, UMTS, HSDPA or Wi-Fi)?

Thanks

Waypoint
  • 17,283
  • 39
  • 116
  • 170

3 Answers3

6

Yes, I use isReachable.

public class Extras {
    public static class Internet {
        public static boolean isOnline() {
            try {
                InetAddress.getByName("google.ca").isReachable(3);
                return true;
            } catch (UnknownHostException e){
                return false;
            } catch (IOException e){
                return false;
            }
        }
    }
}
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
  • 1
    Thanks, but i my case I am getting some informatin relating GPS, and when there is lost of GSM connection, I got bad bevahiour. I could only add it into while(1) loop and check continuously if the internet target is reachable, which could "eat" from my FUP data limit. I was wondering if there is some internal checking, like, is Internet connection accessible do something, if not don't do anything – Waypoint Apr 23 '11 at 14:42
3

I use this in one of my apps:

private boolean isOnline()  {
    ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    return (ni != null && ni.isConnected());
}

You will need these permissions in your Manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Katy Montgomerie
  • 436
  • 4
  • 11
  • 2
    Does that return if there is a network connection or really if that network is connected to Internet? – ForceMagic Oct 10 '13 at 15:54
  • ForceMagic mean if it return false if your device is connected to router without network connection, or if your device is connected to capative network or real network – murt Sep 26 '17 at 09:51
1

You can try retreaving the Local IP address to check whether device is connected to Internet.

for (Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); enumeration.hasMoreElements();) {
        NetworkInterface networkInterface = enumeration.nextElement();
        for (Enumeration<InetAddress> enumIpAddress = networkInterface.getInetAddresses(); enumIpAddress.hasMoreElements();) {
            InetAddress iNetAddress = enumIpAddress.nextElement();
            if (!iNetAddress.isLoopbackAddress()) {
                return iNetAddress.getHostAddress().toString();
            }
        }
    }

The return iNetAddress.getHostAddress().toString(); will give you the IP address. You need to add permission

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

Note: If you are working in Emulator it will retun the emulator IP (generally it will be 10.0.2.15)

James
  • 2,383
  • 2
  • 14
  • 6
  • 3
    If you are connected to a Wifi network this would still give you a local IP but your Wifi router may not be connected to the internet. – source.rar Jun 10 '11 at 14:32