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

3

1

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?

Tapp answered 11/4, 2012 at 3:53 Comment(0)
R
2
    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

Rett answered 16/5, 2012 at 11:28 Comment(0)
B
1

Use ConnectivityManager.TYPE_WIFI

ConnectivityManager connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToNetwork = (networkInfo != null && networkInfo.isConnected());
Behead answered 11/4, 2012 at 4:45 Comment(1)
Hi. Thanks for the reply. However, the above will only check if the device is connected to a wifi interface. It won't check if an internet connection is available.Tapp
I
0
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;
}
Incommodious answered 11/4, 2012 at 12:35 Comment(3)
Thanks for the reply. However, isConnected() only checks if the device is connected to a network. It can't determine if the device has internet connection or none. For example, if you are connected to router, isConnected() will always return true regardless if an internet connection is present or not.Tapp
if you make it sure then you can also make a thread which can ping a site for better accuracy, if you are using internet via gprs then you can check connectivity with PhoneStateListner..Incommodious
@Tapp isConnected() does not only check if the device is connected to the router, but according to the documentation: "Indicates whether network connectivity exists and it is possible to establish connections and pass data.Always call this before attempting to perform data transactions."Buffet

© 2022 - 2024 — McMap. All rights reserved.