如何检查互联网连接的可用性,如果设备连接到路由器? [英] How to check for internet connection availability if device is connected to a router?

查看:171
本文介绍了如何检查互联网连接的可用性,如果设备连接到路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何检查是否一个Android设备连接到Internet连接?目前,我用下面的code:

  ConnectivityManager connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE);
的NetworkInfo NETWORKINFO = connectivityManager.getActiveNetworkInfo();
布尔isConnectedToNetwork =(NETWORKINFO = NULL&放大器;!&安培; networkInfo.isConnected());

然而,与上面的code中的问题是,如果该设备被连接到网络,它仅仅检查。它不检查,如果互联网连接可用。例如,如果设备连接,没有互联网连接到路由器,它仍然会因为从技术上说,您连接到通过路由器的网络返回 isConnectedToNetwork 真。这只是该路由器没有互联网连接。

这是我见过的建议是尝试连接到一个网站上,有一个非常低的被向下机会。这样的一个例子是www.google.com。不过,我觉得这是不是这样的妥善解决。首先,如果用户使用的是GPRS和每KB收取他的互联网的使用,那么他将肩负这个额外的费用。第二,我不认为这是道德的使用第三方网站这样。这真的是检查互联网连接的唯一方式,也可以提出不同的解决办法?难道真的好吗连接到谷歌一样,没有他们的同意?如何检查设备是否有互联网连接?


解决方案

 公共布尔isOnline(上下文的背景下){
    尝试{
        ConnectivityManager厘米=(ConnectivityManager)上下文.getSystemService(Context.CONNECTIVITY_SERVICE);
        如果(cm.getActiveNetworkInfo()。isConnectedOrConnecting()){
            网址URL =新的URL(http://www.ripansekhon.blogspot.com);
            HttpURLConnection的urlc =(HttpURLConnection类)网址.openConnection();
            urlc.setRequestProperty(用户代理,测试);
            urlc.setRequestProperty(连接,关闭);
            urlc.setConnectTimeout(1000); // mTimeout以秒为单位
            urlc.connect();
            如果(urlc.getResponse code()== 200){
                返回true;
            }其他{
                返回false;
            }
        }
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    返回false;
}

检查此功能,以检查是否互联网是工作或没有,这意味着网站开放与否
希望这code有助于所有谁愿意互联网是工作或没有网络之外连接或不以人

How do you check if an Android device is connected to an internet connection? Currently, I am using the code below:

ConnectivityManager connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
boolean isConnectedToNetwork = (networkInfo != null && networkInfo.isConnected());

However, the problem with the code above is that it only checks if the device is connected to a network. It does not check if an internet connection is available. For example, if the device is connected to router with no internet access, it will still return true for isConnectedToNetwork since technically, you are connected to a network via the router. It is just that the router doesn't have an internet connection.

A suggestion that I've seen is to try connecting to a website which has a very low chance of being down. An example of this is www.google.com. However, I think this is not the proper solution for this. First, if the user is using GPRS and is charge per KB for his internet usage, then he will be shouldering an additional cost for this. Second, I don't think it's ethical to use a third party web site like this. Is this really the only way to check for internet connection or can you suggest a different solution? Is it really okay to connect to Google like that without their consent? How can I check if the device has an internet connection?

解决方案

    public boolean isOnline(Context context) {
    try {
        ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); 
        if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
            URL url = new URL("http://www.ripansekhon.blogspot.com");
            HttpURLConnection urlc = (HttpURLConnection) url .openConnection();
            urlc.setRequestProperty("User-Agent", "test");
            urlc.setRequestProperty("Connection", "close"); 
            urlc.setConnectTimeout(1000); // mTimeout is in seconds
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                return true;
            } else {
                return false;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

check this function, to check whether internet is working or not, means website is opening or not Hope this code helps all the people who wants internet is working or not besides network is connected or not

这篇关于如何检查互联网连接的可用性,如果设备连接到路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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