【发布时间】:2017-10-20 08:21:30
【问题描述】:
我有一个测试 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 google 的原因,因为如果我们也登录了我需要。我注意到在很多情况下,即使我没有登录强制门户,代码 sill 到达 google 并返回 true。我也可以上网服务器,例如 ftp。 有谁知道这种行为的原因是什么?有其他人注意到这种行为吗?
谢谢,
【问题讨论】:
-
我没有看到你在 ping 谷歌。也没有登录。也不是强制门户。不管那是什么。
标签: android wifi captivenetwork