我有一个测试wifi连接是否可用的代码。
public boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(3000);
urlc.setReadTimeout(4000);
urlc.connect();
if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
Log.i(TAG,"Internet connection is OK");
return true;
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
Log.i(TAG, "No internet connection.");
return false;
}
要访问互联网,我必须通过一个接入点,这就是为什么我特别 ping 谷歌,因为如果我们也登录了我需要。我注意到在很多情况下,即使我没有登录强制门户,代码sill 到达谷歌并返回 true。我也可以上网服务器,例如 ftp。有谁知道这种行为的原因是什么?有没有其他人注意到这种行为?
谢谢,