In a android studio project I want to check active internet, for that I use this class. Its working but. problem is it only work when connection not available. But i want only return true when connection is active and workable. Because the app working with webview, and if connection is week or not active then its showing not reachable error. Its happening most with WiFi. Please help
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
android.net.NetworkInfo wifi =
cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile =
cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null &&
wifi.isConnectedOrConnecting()))
{
return true;
}
else {
return false;
}
} else{
return false;
}
}
This is what I am using in my working code:
Remember to add in your manifest the network state permission
and, if you need to use internet in your application, consider adding also this:
you can also refer to this link for the official documentation about network permissions hope this helps