0

This code works:

public static boolean isConnected()
{
    ConnectivityManager cm = (ConnectivityManager)App.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni != null) {
        if (ni.getType() == ConnectivityManager.TYPE_WIFI)
            if (ni.isConnected())
                return true;
        if (ni.getType() == ConnectivityManager.TYPE_MOBILE)
            if (ni.isConnected())
                return true;
        if (ni.getType() == ConnectivityManager.TYPE_ETHERNET)
            if (ni.isConnected())
                return true;
    }
    return false; //none of connections available
}

The question is: do we also have to check TYPE_MOBILE_DUN, TYPE_WIMAX and TYPE_VPN?

Can a device be connected to the Internet over Bluetooth?

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
activity
  • 2,653
  • 3
  • 20
  • 44
  • A good and clean answer is [**here**](https://stackoverflow.com/a/44741397/7756492), with step by step implementation guide – ucMedia Apr 20 '18 at 06:06

4 Answers4

1

Just one comment. Think what do you need and remember to be connected to a wifi router doesn't mean you have internet connection or that you are able to reach any point of interest like a backend server. If your app needs to access a service to work, may be the best way it is to check if you can reach it in an early stage through an async call and only proceed if you could validate that connection.

Walter Palladino
  • 479
  • 1
  • 3
  • 8
1

Try to make a simple GET request to http://www.google.com. If your response code is 200 or 400 Then the internet connection exists.

protected static boolean hasInternetAccess()
{
    try
    {
        URL url = new URL("http://www.google.com");

        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        urlc.setRequestProperty("User-Agent", "Android Application:1");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1000 * 30);
        urlc.connect();

        // http://www.w3.org/Protocols/HTTP/HTRESP.html
        if (urlc.getResponseCode() == 200 || urlc.getResponseCode() > 400)
        {
            // Requested site is available
            return true;
        }
    }
    catch (Exception ex)
    {
        // Error while trying to connect
        return false;
    }
    return false;
}

For more info, refer to: The perfect function to check Android internet connectivity including bluetooth pan

MathieuMaree
  • 7,453
  • 6
  • 26
  • 31
Nandhakumar Appusamy
  • 1,156
  • 13
  • 22
0

This is all I use:

public static boolean isOffline() {
    ConnectivityManager cm = (ConnectivityManager) BigOvenApplication.getInstance()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    return netInfo == null || !netInfo.isConnected();
}

I don't think you need anything more than that.

Chantell Osejo
  • 1,456
  • 15
  • 25
0

Just call the method isConnectedToNetwork to check whether it has connection or not. Write this method in a common class file. Thereby you can use simple methodcall where ever you need.

public static boolean isConnectedToNetwork(Context thisActivity) {
        ConnectivityManager connMgr = (ConnectivityManager) thisActivity.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
        if (activeInfo != null && activeInfo.isConnected()) {
            return true;
        }
        return false;
    }

Check before you start the operation. //thisActivity means getActivity() for fragments

   if (isConnectedToNetwork(thisActivity)) {
           // your operation code follows
     } else {
          //show alert box that there is no internet connection
     }
Priya Rajan
  • 687
  • 8
  • 21