如何检查真正的互联网连接的android? [英] how to check the real Internet connected in android?

查看:142
本文介绍了如何检查真正的互联网连接的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个节目,我必须用wifi连接互联网。我找了一些资料,以检查是否在WiFi连接与否。但在某些情况下,您可以连接的无线接入点,但你仍然不能像WiFi的需要账号和密码,证书的HTTPS,或者无线AP无法上网使用Internet。所以,我怎么可以检查真正的互联网连接?

I make a program, and I have to use wifi to connect Internet. I find some information to check whether the wifi is connected or not. But in some situation, you can connect the wifi AP but you still can't use Internet like the wifi needed account and password to certificate in https, or the wifi AP is not able to Internet. So, how can I check the real Internet connected?

推荐答案

只是做一个平到www.google.com的机会,他们是下来是非常低的。

Just do a "Ping" to www.google.com chances that they are down are very low.

P.S。这是我们做我们的应用程序。

P.S. it's what we do in our app..

public static boolean isReachable(Context context) {
    //  First, check we have connectivity
    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = connMgr.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnected()) {
        //  check if google is reachable
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(10 * 1000); // Ten seconds timeout in milliseconds
            urlc.connect();
            if (urlc.getResponseCode() == 200) { // success
                return true;
            } else { // Fail
                return false;
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            return false;
        }
    } else {
        return false;
    }
}

这篇关于如何检查真正的互联网连接的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆