【问题标题】:Android check internet connection is working not just showing connected?Android 检查互联网连接是否正常工作,而不仅仅是显示已连接?
【发布时间】:2016-06-02 10:38:54
【问题描述】:

您好,我知道如何通过参考官方网站链接来检查互联网连接是否可用: https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#DetermineType

但我需要确定给定的互联网连接是否真的能够加载 www.google.com 或任何网站。

让我解释一下这个场景,我确实有一个名为“Wifi-X”的 wifi 路由器,我可以连接到这个路由器,但无法加载网页 www.google.com 或任何其他网站。

现在官方开发者网站(上面的链接)上解释的方法显示已连接,即使我刚刚连接到路由器也无法通过网络 ping 网站。

那么有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: java android internet-connection


    【解决方案1】:

    要检查天气,您实际上可以连接到互联网,您可能想尝试以下方法:

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

    请记住,这不能在您的主线程上运行。

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2011-09-02
      相关资源
      最近更新 更多