Code example for ConnectivityManager: Querying Network Reachability

Methods: getActiveNetworkInfogetBackgroundDataSetting

0
     * @return boolean True/false regarding if the network is available 
     */ 
 
    public static boolean isNetworkAvailable(final Context c) {
        ConnectivityManager connMan = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connMan.getActiveNetworkInfo() != null) {
            NetworkInfo networkInfo = connMan.getActiveNetworkInfo();
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true; 
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE && connMan.getBackgroundDataSetting()) {
                return true; 
            } 
        } 
        return false; 
    } 
 
    /* 
     * Author: Karl Lindmark 
     * @param Context The context to be called from 
     * @return String The path to the cache directory 
     */