I am trying to detect if there are any active internet connections (WIFI or MOBILE INTERNET). The code is as follows:
private boolean haveNetwork() {
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean flag=false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (Network network : connMgr.getAllNetworks()) {
NetworkInfo networkInfo = connMgr.getNetworkInfo(network);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
flag=true;
break;
} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
flag=true;
break;
}
}
}
Log.d("flag", String.valueOf(flag)); //returns true even if there's no internet connection
return flag;
}
As the title says, my connection status always remains to be true, even if there is no internet connection