Java tutorial
//package com.java2s; import java.util.List; import java.util.Map; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class Main { public static boolean isConnToInternet(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] infoArr = connectivity.getAllNetworkInfo(); if (isNotEmpty(infoArr)) { for (NetworkInfo networkInfo : infoArr) { if (networkInfo.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } public static boolean isNotEmpty(Object array) { return !isEmpty(array); } @SuppressWarnings("rawtypes") public static boolean isEmpty(Object array) { if (null != array) { if (array instanceof List) { List list = (List) array; if (!list.isEmpty()) { return false; } } else if (array instanceof Map) { Map map = (Map) array; if (!map.isEmpty()) { return false; } } else if (array instanceof Object[]) { Object[] arr = (Object[]) array; if (arr.length > 0) { return false; } } } return true; } }