[英]Android check internet connection when 3g is available but not accessible
我一直在寻找其他问题的帮助,但找不到任何东西,而在审查活动中需要互联网连接,即使激活了3G,但也无法连接(超过了数据使用或防火墙的使用)教程或我发现的帮助不包括在内。
我使用此小功能来检测要登录页面的公共Wifi重定向。 如果此函数返回false,则意味着即使打开3G或Wifi,也无法连接到预期的页面。 这样,您可以向用户显示任何页面,例如“无Internet连接”页面。
public boolean networkSignOn() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://clients3.google.com/generate_204");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
urlConnection.setUseCaches(false);
urlConnection.getInputStream();
return urlConnection.getResponseCode() == 204;
} catch (IOException e) {
Log.v("Walled garden check - probably not a portal: exception " + e, "");
return false;
} finally {
if (urlConnection != null) urlConnection.disconnect();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.