How to check the availability of an Internet connection if the device is connected to a router?

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

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 checks if the device is connected to the network. It does not check for an internet connection. For example, if a device is connected to a router without Internet access, it will still return true for isConnectedToNetwork, because from a technical point of view you are connecting to the network through a router. It’s just that the router does not have an Internet connection.

The suggestion I saw was to try connecting to a website that has a very low chance of a fall. An example of this is www.google.com. However, I think this is not the right solution for this. Firstly, if the user uses GPRS and charges a fee for KB for using the Internet, then he will charge an additional fee for this. Secondly, I don’t think it is ethical to use a third-party website like this. Is this really the only way to test your Internet connection or suggest a different solution? Is it really normal to connect to Google without their consent? How to check if the device is connected to the Internet?

+1
source share
3 answers
    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;
}

, , , , - , , , , , .

+2

ConnectivityManager.TYPE_WIFI

ConnectivityManager connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToNetwork = (networkInfo != null && networkInfo.isConnected());
+1
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;
}
0
source

All Articles