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?