3

I am trying to check if there is an internet connectivity or if the device is actually connected to wifi.

Note: I am not trying to see if there is internet connectivity or if just the wifi is connected. I want to check if the device is actually receiving and sending the packets.

This code snippet actually checks if there is a mobile data connectivity or if the wifi is enabled but it doesn't say if the connection is actually established.

public boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();            
        if (ni == null) {
            // There are no active networks.
            Toast.makeText(context, "No Network", Toast.LENGTH_LONG).show();
            return false;
        } else
            return true;

OR

    boolean isConnected = ni != null && ni.isConnectedOrConnecting();

    if(isConnected){

        return true;

    }
    else{
        Toast.makeText(context, "No Network", Toast.LENGTH_LONG).show();
        return false;
    }
}

or

public boolean haveNetworkConnection(Context context) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

All the above code snippets only checks if the wifi or data is enabled. It doesn't show if the connection is established. It will always return true if the wifi or data is enabled.

Again, my goal is to check is to check if the connection is established.

Any help will be really helpful.. Thank's in advance..:)

This question has been answered. Thanks again.

mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • For my understanding & clarity, I believe one should be able to have a connection only if a request to connect with a server is made. What sort of connection establishment do you want here? – Sameer Khan Apr 18 '14 at 17:13
  • I just want to check if the internet is actually available to the user. Because if the mobile data or a wifi is just enabled, this code will return true every time. The connection should be made as in, U should be able to access internet. Sometimes what happens is that you have the mobile data settings on, and u don't have a data plan. – mike20132013 Apr 18 '14 at 19:57

2 Answers2

5

Thanks for the comments and answer guys but I think this solution is working for me. This is actually trying to connect to internet and checking the status. Not sure if this is the right way to do it.

public Boolean isOnline() {
        try {
            Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
            int returnVal = p1.waitFor();
            boolean reachable = (returnVal==0);
            if(reachable){
                System.out.println("Internet access");
                return reachable;
            }
            else{
                System.out.println("No Internet access");
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
        return false;
    }

Hope this helps other viewers.

mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • 1
    I had some issues running this onCreate, the UI would hang. I recommend running this in an AsyncTask. –  Dec 06 '14 at 18:32
  • True.. this is a bit extensive task to do in the main thread. Sometimes it does freezes the UI for few seconds to do the check. – mike20132013 Dec 08 '14 at 15:33
  • @mike20132013 I think you meant to say **expensive** not **extensive** :) – Peter Chaula Aug 21 '17 at 09:54
0

You have to ping a server on the internet(ie. google) to see if you're actually connected.

rw.liang1
  • 418
  • 4
  • 12