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"/>
3

4 Answers 4

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;
}
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;
}
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).

4
  • please include toasts in your answer. Nov 6, 2015 at 8:45
  • Ok, but check log if you can?
    – Androider
    Nov 6, 2015 at 8:47
  • your answer is full of errors :( can you please edit it and give me the full implementation? also i want to tun this in oncreate Nov 6, 2015 at 8:48
  • please, show me errors. and i recommend to do some self stuffs.
    – Androider
    Nov 6, 2015 at 8:50
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;
    }

}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.