w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Effective way to check for slow internet connection and differentiate between network connection and actual internet connection in android

Something like this would work:

private boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager)
        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;

I have copied this code from here (I understand the code - I am just adding the code here for ease for the user + I got told by an admin that you should do this and credit the original post). Personally I would split this into two functions, one for mobile and one for wifi, but that choice is up to you.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.