如何在网络中的机器人内检查因特网连接(使用通过HOTSPOT某些其他设备的网络) [英] How to check the internet connectivity within the network in Android (using internet of some other device through HOTSPOT)

查看:126
本文介绍了如何在网络中的机器人内检查因特网连接(使用通过HOTSPOT某些其他设备的网络)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里,我要检查是否有当我与网络已经连接任何网络连接的要求。

I have a requirement where I want to check whether there is any internet connectivity when I am connected with the network.

例如,我有设备A和设备B。

For example, I have device A and device B.

设备A与热点与设备B在设备A的连接,我得到它与无线网络连接和设备B连接 - 与热点连接一个设备

Device A is connected with hotspot with that of device B. In device A, I get it as connected with Wi-Fi and in device B - one device connected with hotspot.

现在,如果我从设备B(而不是圈养热点)删除Internet,然后在设备A,它仍然显示 - 通过Wi-Fi连接,但没有互联网连接。

Now, if I remove the internet from device B (not the tethering hotspot), then in device A, it still shows - connected with Wi-Fi but there is no internet connectivity.

像ConnectivityManager帮助类的确定与不看好互联网连接网络的设备是否连接。

Classes like ConnectivityManager help in determining whether a device is connected with the network not about the internet connectivity.

我想跟踪此问题。有什么办法来实现这一目标?

I want to track this issue. Is there any way to achieve this?

推荐答案

它可以是一个荒谬的解决方案,但我想也可能是真正的解决方案:

It can be a ridiculous solution but i think also it could be real solution:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return new Boolean(true);
            }
        } catch (MalformedURLException mue) {
            // TODO Auto-generated catch block
            mue.printStackTrace();
        } catch (IOException ie) {
            // TODO Auto-generated catch block
            ie.printStackTrace();
        }
    }
    return false;
}

这篇关于如何在网络中的机器人内检查因特网连接(使用通过HOTSPOT某些其他设备的网络)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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