Android check internet network connection

www.lau‮‬tturi.com
Android check internet network connection
public boolean isOnline() {
    /*Just to check Time delay*/
    long t = Calendar.getInstance().getTimeInMillis();

    Runtime runtime = Runtime.getRuntime();
    try {
        /*Pinging to Google server*/
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        long t2 = Calendar.getInstance().getTimeInMillis();
        Log.i("NetWork check Time", (t2 - t) + "");
    }
    return false;
}

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

boolean isNetworkAvailable = false;
boolean isMobileNetworkAvailable= connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
boolean isWifiEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();

if (isWifiEnable() || isMobileNetworkAvailable()) {
    /*Sometime wifi is connected but service provider never connected to internet,so cross check one more time*/
    if (isOnline())
        isNetworkAvailable = true;
}
Created Time:2017-08-30 15:45:26  Author:lautturi