Android Network State Check getNetworkState(Context context)

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

Description

get Network State

Declaration

public static int getNetworkState(Context context) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

public class Main {
    public final static int NONE = 0;
    public final static int WIFI = 1;
    public final static int MOBILE = 2;

    public static int getNetworkState(Context context) {
        if (null != context) {
            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            State state;//from  w  ww.  j av a2s. com
            NetworkInfo networkInfo;
            if (null != connManager) {
                //Wifi????
                networkInfo = connManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                if (null != networkInfo) {
                    state = networkInfo.getState();
                    if (state == State.CONNECTED
                            || state == State.CONNECTING) {
                        return WIFI;
                    }
                }

                //3G????
                networkInfo = connManager
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (null != networkInfo) {
                    state = networkInfo.getState();
                    if (state == State.CONNECTED
                            || state == State.CONNECTING) {
                        return MOBILE;
                    }
                }
            }

        }
        return NONE;
    }
}

Related

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