Android app opens and crashes when there is no internet

You can use this function to check internet avaibility.

public boolean isInternetAvailable(Context context) {
            boolean isAvailable;
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            isAvailable = cm.getActiveNetworkInfo() != null;
            return isAvailable;
        }

Use Example:

if (isInternetAvailiable(context)) {
    //network request here
} else {
    //toast no internet connection
}

Browse More Popular Posts

Leave a Comment