5
I have an app where I do a connection test before consulting a webservice, just to display a message to user who has no internet connection. I use a method this way:
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 understood in this question about Testing an application’s Internet connection which is used the Context.CONNECTIVITY_SERVICE
, as in my job. Based on some tests I did, this way is done, if it is only connected to WIFI or 4G, It is detected that has a connection, but not always works, because in some cases ends up falling in a INTRANET. It may be an efficient way, but not necessarily as effective.
I saw that answer about Texte de Contexão that it is possible to give a ping in the HOST, but according to the validated answer, not always the hosts accept pings.
I try all this questioning, what would be the most effective way to test if there is an internet connection?
I had forgotten this question! Thank you. = D
– viana