W3docs

How to check internet access on Android? InetAddress never times out

To check for internet access on Android, you can use the isReachable() method of the InetAddress class.

To check for internet access on Android, the recommended approach is to use ConnectivityManager. The InetAddress.isReachable() method is unreliable on Android, often blocked by firewalls, and does not verify actual internet connectivity, which can lead to false positives or indefinite timeouts.

You can check connectivity by querying the active network info. Note that this requires the ACCESS_NETWORK_STATE and INTERNET permissions in your AndroidManifest.xml.

Here's an example of how to check for internet access on Android:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {
    public static boolean isInternetAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }
}

// Usage example:
// if (isInternetAvailable(context)) {
//     System.out.println("Internet is available");
// } else {
//     System.out.println("Internet is not available");
// }

I hope this helps! Let me know if you have any questions.