Example usage for android.net NetworkInfo getState

List of usage examples for android.net NetworkInfo getState

Introduction

In this page you can find the example usage for android.net NetworkInfo getState.

Prototype

@Deprecated
public State getState() 

Source Link

Document

Reports the current coarse-grained state of the network.

Usage

From source file:com.emacs.xpets.utils.Utils.java

public static boolean isNetworkAvailable(Context context) {
    boolean status = false;
    try {/*from  ww  w .  j  a  v  a  2 s  .com*/
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            status = true;
        } else {
            netInfo = cm.getNetworkInfo(1);
            if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
                status = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return status;
}

From source file:com.musevisions.android.SudokuSolver.HttpPostUtils.java

static public boolean isConnected(Activity parent) {
    ConnectivityManager cm = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED;
}

From source file:Main.java

public static String getNetworkConnType(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (null == connectMgr) {
        return null;
    }/*from ww w .  j  a  va  2  s  .  co  m*/

    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return null;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return null;
    }

    NetworkInfo mobileNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (null == mobileNetworkInfo) {
        return null;
    }
    NetworkInfo.State mobile = mobileNetworkInfo.getState();
    if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
        return NETWORK_CONN_MOBILE;
    }

    NetworkInfo wifiNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (null == wifiNetworkInfo) {
        return null;
    }
    NetworkInfo.State wifi = wifiNetworkInfo.getState();
    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
        return NETWORK_CONN_WIFI;
    }
    return null;
}

From source file:org.gplvote.signdoc.MainActivity.java

public static boolean isInternetPresent(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (NetworkInfo anInfo : info)
                if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }// w  w  w  .j a  va 2s. co m
    }
    return false;
}

From source file:dev.application.taxivip.helpers.LocationUtils.java

/**
 * Verifico que halla al menos una conexion activa a internet
 * @param context/*from   ww  w. j  av a2 s  .  c o  m*/
 * @return true o false
 */
public static boolean networkAvailable(Context context) {
    //Context context = getApplicationContext();
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectMgr != null) {
        NetworkInfo[] netInfo = connectMgr.getAllNetworkInfo();
        if (netInfo != null) {
            for (NetworkInfo net : netInfo) {
                if (net.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }

    return false;
}

From source file:Main.java

public static int getCurrentNetType(Context context) {

    int result = NETTYPE_NONE;

    NetworkInfo localNetworkInfo = getCurrentActiveNetworkInfo(context);

    if (localNetworkInfo == null) {

        return result;

    }/*w  ww.j a  v  a 2  s . co m*/

    if (localNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {

        if (localNetworkInfo.getType() == 1) {

            result = NETTYPE_WIFI;

        } else if (localNetworkInfo.getType() == 0) {

            String subTypeName = localNetworkInfo.getSubtypeName().toUpperCase(Locale.getDefault());

            if (subTypeName.indexOf("GPRS") > 1) {

                result = NETTYPE_MOBILE_GPRS;

            } else if (subTypeName.indexOf("EDGE") > 1) {

                result = NETTYPE_MOBILE_EDGE;

            } else {

                result = NETTYPE_MOBILE_3G;

            }

        } else {

            result = NETTYPE_UNKNOW;

        }

    } else if (localNetworkInfo.getState() == NetworkInfo.State.CONNECTING) {

        result = NETTYPE_UNKNOW;

        System.out.println("connecting " + localNetworkInfo.getType());

    }

    return result;

}

From source file:com.photon.phresco.nativeapp.eshop.net.NetworkManager.java

public static boolean checkNetworkConnectivity(final Activity activity) {
    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else if (netInfo != null && (netInfo.getState() == NetworkInfo.State.DISCONNECTED
            || netInfo.getState() == NetworkInfo.State.DISCONNECTING
            || netInfo.getState() == NetworkInfo.State.SUSPENDED
            || netInfo.getState() == NetworkInfo.State.UNKNOWN)) {
        return false;
    } else {//ww w.  j av a 2s.com
        return false;
    }
}

From source file:net.peterkuterna.android.apps.devoxxsched.service.SyncService.java

/**
 * Are we connected to a WiFi network?//w ww.ja v a  2  s.c  o m
 */
private static boolean isWifiConnected(Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager != null) {
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return (networkInfo != null && networkInfo.getState().equals(State.CONNECTED));
    }

    return false;
}

From source file:hobby.wei.c.phone.Network.java

public static Type getConnectedNetworkType(Context context) {
    NetworkInfo networkInfo = getConnectManager(context).getActiveNetworkInfo();
    if (networkInfo == null || networkInfo.getState() != NetworkInfo.State.CONNECTED) {
        return Type.NO_NET;
    }//w  w  w .  jav a2s  .  c  o  m
    return getType(networkInfo);
}

From source file:org.kontalk.util.SystemUtils.java

/** Checks for network availability. */
public static boolean isNetworkConnectionAvailable(Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getBackgroundDataSetting()) {
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null && info.getState() == NetworkInfo.State.CONNECTED)
            return true;
    }//from   ww w.j  a v  a  2  s. c o  m

    return false;
}