简体   繁体   English

Android检查互联网连接是否在工作,不仅显示已连接?

[英]Android check internet connection is working not just showing connected?

Hi I know how to check the internet connection available or not by referring to official site link : https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#DetermineType 嗨,我知道如何通过参考官方站点链接检查互联网连接是否可用: https : //developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#DetermineType

But I need to find if the given internet connection is actually capable of loading www.google.com or any web site. 但是,我需要确定给定的互联网连接是否真的能够加载www.google.com或任何网站。

Let me explain the scenario, I do have wifi router named "Wifi-X" I am able to connect to this router but unable to load the web page www.google.com or any other site. 让我解释一下这种情况,我确实有一个名为“ Wifi-X”的wifi路由器,我可以连接到该路由器,但是无法加载网页www.google.com或任何其他站点。

Now method explained on the official developer site (above link) shows connected even if I am just connected to router unable to ping the websites over the web. 现在,即使我只是连接到无法通过Web ping网站的路由器,在官方开发人员网站(上面的链接)上介绍的方法也显示已连接。

So is there any solution for this problem ? 那么这个问题有什么解决办法吗?

To check weather you can actually connect to the internet, you might want to try something as such: 要检查天气,您实际上可以连接到互联网,您可能需要尝试以下方法:

public static boolean isInternetReachable() {
    InetAddress inet;
    try {
        inet = InetAddress.getByName("www.google.com");
        return true;
    } catch (UnknownHostException e) {
        return false;
    }
}

Do remember that this cannot run on your main thread. 请记住,这不能在您的主线程上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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