简体   繁体   中英

Android check internet connection when 3g is available but not accessible

我一直在寻找其他问题的帮助,但找不到任何东西,而在审查活动中需要互联网连接,即使激活了3G,但也无法连接(超过了数据使用或防火墙的使用)教程或我发现的帮助不包括在内。

I use this little function to detect Public Wifi redirects to sign on page. If this function returns false, it means you could not connect to the intended page despite having your 3G or Wifi on. With this you can show the user any page, for example a "No Internet Connection" page.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM