Android Network State Check isNetworkAvailable(Context context)

Here you can find the source of isNetworkAvailable(Context context)

Description

Checks device for network connectivity

Return

If the device has data connectivity

Declaration

public static boolean isNetworkAvailable(Context context) 

Method Source Code

//package com.java2s;
import android.content.Context;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Main {
    private static final String TAG = Thread.currentThread()
            .getStackTrace()[1].getClassName();

    /**//  ww  w  .ja va  2s .  com
     * Checks device for network connectivity
     *
     * @return If the device has data connectivity
     */
    public static boolean isNetworkAvailable(Context context) {
        boolean state = false;
        if (context != null) {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()) {
                Log.i(TAG, "The device currently has data connectivity");
                state = true;
            } else {
                Log.i(TAG,
                        "The device does not currently have data connectivity");
                state = false;
            }
        }
        return state;
    }
}

Related

  1. isNetworkConnected(Context context)
  2. isNetworkEnabled(Context context)
  3. check3GNetwork(Context context)
  4. IsNetworkConnected(Context context)
  5. isNetworkAvailableExt(Context paramContext)
  6. getNetworkState(Context context)
  7. isLocationEnabledNetwork(Context c)
  8. isOpenNetwork(Context context)
  9. loadImageFromNetwork(String photoUrl)