I am developing an android application and I need to check whether the device is really connected to the internet using the following the method
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(10 * 1000); // Ten seconds timeout in milliseconds
urlc.connect();
if (urlc.getResponseCode() == 200) { // success
System.out.println("success mobile");
return true;
} else { // Fail
return false;
}
} catch (IOException e) {
return false;
}
}
else
{
return false;
}
this method works if I disable the mobile data or the wifi. However, in my case the provider simply stopped the internet because I've reached my usage limit or run out of data allowance. So in this case the method returns TRUE even though when I go to the browser and type in google.com it doesn't work and I get the message by the provider that I've reached my limit? So somehow google.com pings as reachable even though I can't open it from the device due to the usage limit? I can only access the webpage of my mobile provider where I can buy more allowance so the internet is not switched off but just limited. How can I check for that?