Java tutorial
//package com.java2s; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class Main { public static boolean isNetworkAvailable(Context context) { if (context == null) { Log.e("isNetworkAvailable", " Context is null"); return false; } try { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getActiveNetworkInfo(); return info.isAvailable(); } } catch (Exception e) { return false; } return false; } }