如何检查android中连接的真实互联网?

aaa*_* SA 6 android wifi

我制作一个程序,我必须使用wifi连接互联网.我找到一些信息来检查wifi是否已连接.但在某些情况下,你可以连接wifi AP,但你仍然无法使用互联网,如WiFi所需的帐户和密码到https的证书,或无线AP无法上网.那么,我该如何检查真正的互联网连接?

Fra*_*ank 5

只需对www.google.com进行"Ping",他们失败的可能性非常低.

PS这是我们在我们的应用程序中做的..

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;
    }
}
Run Code Online (Sandbox Code Playgroud)