How to check for internet connection availability if device is connected to a router?

♀尐吖头ヾ 提交于 2019-12-11 07:15:12

问题


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?


回答1:


    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




回答2:


Use ConnectivityManager.TYPE_WIFI

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



回答3:


public  boolean checkInternetConnection() {

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        Log.d("Internet Connection  Present","");
        isFound=true;
    } else {
        Log.d("Internet Connection Not Present","");
        isFound= false;
    }
    return isFound;
}


来源:https://stackoverflow.com/questions/10099564/how-to-check-for-internet-connection-availability-if-device-is-connected-to-a-ro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!