Rishab Parmar
Rishab Parmar

Reputation: 419

Connected, no Internet in Android

I have been trying to figure out how to deal with Android: Connected, No Internet condition whether you are connected using Wifi or Cellular Data.

Code:

public boolean checkForInternetConnectivity() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        if(manager != null) {            
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
                // Works when connected to the internet and when internet is available but the fails when no internet
                NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());                    
                return networkCapabilities != null && (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
            }else {               
                NetworkInfo netInfo = null;
                netInfo = manager.getActiveNetworkInfo();
                return netInfo!= null && netInfo.isConnected();
            }
        }
        return false;
}

I just want the above method to return true if the mobile is connected to the internet and internet is available and false if there is no interet or connectivity.

So my questions are - How do I deal with the 'No internet' condition? Is there a built-in method available for doing this? Or am I missing something in the mentioned code itself?

Upvotes: 2

Views: 250

Answers (2)

Akram Bensalem
Akram Bensalem

Reputation: 101

Make sure to add this line in your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

while you will us this methode in a diffrenet activities I suggest to use a static method bette. add this code:

public static boolean isNetworkAvailable(Context context) {
            boolean HaveConnectWIFI = false;
            boolean HaveConnectMobile = false;

            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
            assert connectivityManager != null;
            NetworkInfo[] activeNetworkInfo = connectivityManager.getAllNetworkInfo();
            for (NetworkInfo ni : activeNetworkInfo) {
                if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                    if (ni.isConnected())
                        HaveConnectWIFI = true;
                if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    if (ni.isConnected())
                        HaveConnectMobile = true;
            }
            return HaveConnectMobile || HaveConnectWIFI;
        }

Upvotes: 0

Sanjay Chauhan
Sanjay Chauhan

Reputation: 913

Replace this code.

public final boolean checkForInternetConnectivity() {
            ConnectivityManager cm = (ConnectivityManager) getSystemService("connectivity");
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            if (activeNetwork != null) {
                if (activeNetwork.getType() == 1) {
                    return true;
                } else {
                    return activeNetwork.getType() == 0;
                }
            } else {
                return false;
            }
        }
    }

Add Permission this android.permission.ACCESS_NETWORK_STATE in the manifest file.

Upvotes: 1

Related Questions