Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.net.ConnectivityManager; import android.widget.Toast; public class Main { private static final boolean YES = true; private static final boolean NO = false; public static boolean isInternetAvailable(Context context) { if (context != null) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting(); } else { return NO; } } public static boolean isInternetAvailable(Context context, String msg) { if (isInternetAvailable(context)) { return YES; } else { showToast(context, msg); return NO; } } public static void showToast(Context context, String msg) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } }