July 31, 2015

Snippet #3. Know if a device is connected to the internet

Sometimes you need or want to know if a device is connected to the internet to prevent (or to do) some action. Maybe the device is connected to some Wifi or maybe it’s to mobile data. If you don’t mind what type of connection it has, and you only want to check if it’s connected to any type of internet you can do it easily with this code:

public static boolean isConnectedToTheInternet(Context context) {
    ConnectivityManager cm = (ConnectivityManager) 
                           context.getSystemService
                           (Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();

    return ni != null && ni.isConnected() && ni.isAvailable();
}
  • context: A valid context.

What does it returns?

true if the the device is connected to the internet, false if not.