I need some method to verify if my network is conected to interne, and during my researchs I found some method describe below:
public static boolean hasInternetAccess(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.e(TAG, "Error checking internet connection", e);
}
} else {
Log.d(TAG, "No network available!");
}
return false;
}
...but my programs will be distributed on China, and Google is blocked on China.
I have on mind to create a method like this selecting first my localization to get a Baidoo web site - like Google on China - but make this is very expansive method to check if device is connected on some network and verify if than connected on Internet.