I have an application in which I test a connection before consulting a webservice , to display a message to a user who does not have an internet connection. I use a method like this:
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
return true;
} else {
return false;
}
}
I noticed in this question about Test internet connection of an application which is used Context.CONNECTIVITY_SERVICE
, as in my function. Based on some tests I did, this way it is done, if it is only connected to the WIFI or 4G, it is detected that it has a connection, but it does not always work, because in some cases it ends up falling into an INTRANET . It may be an efficient, but not necessarily as effective, way.
I've seen this answer about Context Texts that you can give < in HOST , but according to the validated response, hosts do not always accept pings in>.
I try all this questioning, what would be the most effective way to test if there is an internet connection?