0

I have been using ConnectivityManager to detect whether there is an internet connection or not however, it only tells you that regardless of if there is really an internet connectivity or not, i have heard this could be detected by sending pings to https://www.google.com/ . i would also like to show toasts.

ConnectivityManager cmanager =     (ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo info = cmanager.getActiveNetworkInfo();
    if(info!=null && info.isConnected()) {
        if(info.getType() == ConnectivityManager.TYPE_WIFI) {
            Toast.makeText(MainActivity.this, "Wifi", Toast.LENGTH_LONG).show();   
        } else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
            Toast.makeText(MainActivity.this, "mobile", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(MainActivity.this, "Not connected", Toast.LENGTH_LONG).show();
    }
}


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
MaggotSauceYumYum
  • 402
  • 1
  • 5
  • 23

4 Answers4

0

I hope this will help you.

public static boolean thereIsConnection(Context context) {

    if (SystemHelper.isEmulator(context)) {
        return true;
    }

    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo i = conMgr.getActiveNetworkInfo();
    if (i == null) {
        return false;
    }
    if (!i.isConnected()) {
        return false;
    }
    if (!i.isAvailable()) {
        return false;
    }
    return true;
}
alican akyol
  • 303
  • 3
  • 14
0

Very simple like this:

private boolean haveInternet(){
NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info==null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to disable internet while roaming, just return false
return true;
}
return true;
}
xxx
  • 3,315
  • 5
  • 21
  • 40
0

Try this according to your Problem :

    public static boolean hasActiveInternetConnection(Context context) {
        if (isNetworkAvailable(context)) {
            try {
                HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
                urlc.setRequestProperty("User-Agent", "Test");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(1500); 
                urlc.connect();
                return (urlc.getResponseCode() == 200);
            } catch (IOException e) {
                Log.e("Connection Status:", "Error checking internet connection", e);
            }
        } else {
            Log.e("Connection Status:", "No network available!");
        }
        return false;
    }

Don't forget to provide required permissions.

& Don't run it on Main Thread(go for background thread like Asynctask).

Androider
  • 3,833
  • 2
  • 14
  • 24
0

Yes, you should do it by pinging. This is my network util working properly:

    public class NetworkUtil {

        Context context;

        public static boolean isConnected(Context context) {
            return isNetworkAvailable();
        }

        private static boolean isNetworkAvailable() {ConnectivityManager connectivityManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        if(info!=null && info.isConnected() && canAccessToInternet()) {
            if(info.getType() == ConnectivityManager.TYPE_WIFI) {
                Toast.makeText(MainActivity.this, "Wifi", Toast.LENGTH_LONG).show();   
            } else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
                Toast.makeText(MainActivity.this, "mobile", Toast.LENGTH_LONG).show();
            }
            return true;
        } else {
            Toast.makeText(MainActivity.this, "Not connected", Toast.LENGTH_LONG).show();
            return false;    
        }
    }

    private static Boolean canAccessToInternet() {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process ipProcess = runtime.exec("/system/bin/ping -c 1 google.com");
            int exitValue = ipProcess.waitFor();
            return exitValue == 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
Héctor
  • 24,444
  • 35
  • 132
  • 243