How do you check if an Android device is connected to an internet connection? Currently, I am using the code below:
ConnectivityManager connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
boolean isConnectedToNetwork = (networkInfo != null && networkInfo.isConnected());
However, the problem with the code above is that it only checks if the device is connected to a network. It does not check if an internet connection is available. For example, if the device is connected to router with no internet access, it will still return true for isConnectedToNetwork
since technically, you are connected to a network via the router. It is just that the router doesn't have an internet connection.
A suggestion that I've seen is to try connecting to a website which has a very low chance of being down. An example of this is www.google.com. However, I think this is not the proper solution for this. First, if the user is using GPRS and is charge per KB for his internet usage, then he will be shouldering an additional cost for this. Second, I don't think it's ethical to use a third party web site like this. Is this really the only way to check for internet connection or can you suggest a different solution? Is it really okay to connect to Google like that without their consent? How can I check if the device has an internet connection?