Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top