How do you check if an Android device is connected to an internet connection? I am currently using the following code:
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 checks if the device is connected to the network. It does not check for an internet connection. For example, if a device is connected to a router without Internet access, it will still return true for isConnectedToNetwork, because from a technical point of view you are connecting to the network through a router. It’s just that the router does not have an Internet connection.
The suggestion I saw was to try connecting to a website that has a very low chance of a fall. An example of this is www.google.com. However, I think this is not the right solution for this. Firstly, if the user uses GPRS and charges a fee for KB for using the Internet, then he will charge an additional fee for this. Secondly, I don’t think it is ethical to use a third-party website like this. Is this really the only way to test your Internet connection or suggest a different solution? Is it really normal to connect to Google without their consent? How to check if the device is connected to the Internet?
source
share