当3g可用但无法访问时,Android会检查互联网连接

时间:2013-05-14 02:07:07

标签: android connection

我一直在寻找其他问题的帮助,但找不到东西,而在评论活动需要互联网连接,即使激活3G但无法连接(已超出数据使用或防火墙)教程或帮助我发现不包括那个。

1 个答案:

答案 0 :(得分:1)

我使用这个小功能来检测公共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();
    }
}