0

I am using this code to check the connection in my app :

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

this method will return "true" even the device just connect to wifi and not has a connection to the internet or the device turn on mobile network but run out of data. I have read many posts, but most of them are too old. I dont know those answers are still right or not. So, how to check if the device has a "real" connection to the internet ?

This maybe a similar question : Android: Check network and real internet connectivity

  • 1
    How are you defining "real"? Generally, you make a benign request of the server that you are trying to talk to. – CommonsWare May 13 '19 at 16:30
  • I meant : the device is able to browse web or watch some videos...if the device does not have a "real" connection, it will not be able to do these things – Trần Quốc Trung May 13 '19 at 16:33
  • 1
    Make a request of the Web server, then (e.g., request `robots.txt` or something). There is no uniform "the Internet", as the rules for what you can and cannot access vary by destination. For example, if a Chinese user cannot access Wikipedia, does the user have a "real" connection or not? If the app wants to access Wikipedia, the answer would be "no". If the app wants to access some other site, the answer would be "maybe". So, make a request of the specific server you are trying to reach, and react accordingly. Or, simply deal with the error when you make the real request, if it fails. – CommonsWare May 13 '19 at 16:43
  • Thanks. I am trying to check if a device is able to transfer some data. Because of that, if the device does not have a "real" connection, it will never be able to transfer data. – Trần Quốc Trung May 13 '19 at 16:46

0 Answers0