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:fr.mixit.android.utils.NetworkUtils.java

public static ConnectivityState getConnectivity(Context ctx) {
    ConnectivityState currentNetworkType;
    final ConnectivityManager conMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        NetworkInfo networkInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null && networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
            currentNetworkType = ConnectivityState.WIFI;
        } else {//  w w  w .j  a  v a  2 s. c om
            networkInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (networkInfo != null && networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                currentNetworkType = ConnectivityState.CARRIER;
            } else {
                // how can we be there ? no wifi and no mobile data but connected ?
                currentNetworkType = ConnectivityState.UNKNOWN;
            }
        }
    } else {
        currentNetworkType = ConnectivityState.NONE;
    }

    return currentNetworkType;
}

From source file:org.bcsphere.activity.BCPage.java

public static boolean isNetworkConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager != null) {
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }/*from   w w  w.ja  va 2 s  .  com*/
        }
    }
    return false;
}

From source file:com.mpower.mintel.android.utilities.WebUtils.java

public static boolean isConnected(Context context) {
    final ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) {
        return true;
    } else {// w ww.  j  ava  2  s .c  o  m
        return false;
    }
}

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

public static boolean isNetConnected(Context context) {
    NetworkInfo networkInfo = getConnectManager(context).getActiveNetworkInfo();
    if (networkInfo == null) {
        return false;
    }/*from   w  w w .j  ava 2s  . c om*/
    return networkInfo.getState() == NetworkInfo.State.CONNECTED;
}

From source file:com.mpower.daktar.android.utilities.WebUtils.java

public static boolean isConnected(final Context context) {
    final ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED)
        return true;
    else//from   w w  w. j  a  v a  2s . c o  m
        return false;
}

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

public static State getNetworkState(Context context) {
    NetworkInfo networkInfo = getConnectManager(context).getActiveNetworkInfo();
    if (networkInfo == null)
        return State.DISCONNECTED;

    State state = State.UNKNOWN;

    switch (networkInfo.getState()) {
    case CONNECTING:
        state = State.CONNECTING;
        break;/*from   w w  w  .jav a 2s . co  m*/
    case CONNECTED:
        state = State.CONNECTED;
        break;
    case SUSPENDED:
        state = State.SUSPENDED;
        break;
    case DISCONNECTING:
        state = State.DISCONNECTING;
        break;
    case DISCONNECTED:
        state = State.DISCONNECTED;
        break;
    case UNKNOWN:
        state = State.UNKNOWN;
        break;
    }
    return state;
}

From source file:com.fsck.k9.helper.Utility.java

/**
 * Check to see if we have network connectivity.
 *///from w  w w  . ja v a  2 s .  com
public static boolean hasConnectivity(final Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    }
    final NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
        return true;
    } else {
        return false;
    }
}

From source file:com.air.mobilebrowser.NetworkUtil.java

/**
 * Check the current status of internet connectivity.
 * This method iterates over the available network interfaces and
 * checks for an active connection./*from   w w  w.j  a  v  a 2s .  c  o  m*/
 * @return true if a connection was detected, false otherwise.
 */
public static boolean haveInternetConnection(Context context) {
    if (context != null) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiInfo = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wimax = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
        NetworkInfo blue = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
        NetworkInfo ether = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

        boolean hasInternet = false;

        if (wifiInfo != null && wifiInfo.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (wimax != null && wimax.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (blue != null && blue.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (ether != null && ether.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        }

        return hasInternet;
    }

    return false;
}

From source file:com.playhaven.android.req.PlayHavenRequest.java

protected static PlayHaven.ConnectionType getConnectionType(Context context) {
    try {//from  w  w w  . ja v a  2  s. c  o  m
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (manager == null)
            return PlayHaven.ConnectionType.NO_NETWORK; // happens during tests

        NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiInfo != null) {
            NetworkInfo.State wifi = wifiInfo.getState();
            if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
                return PlayHaven.ConnectionType.WIFI;
        }

        NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileInfo != null) {
            NetworkInfo.State mobile = mobileInfo.getState();
            if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
                return PlayHaven.ConnectionType.MOBILE;
        }
    } catch (SecurityException e) {
        // ACCESS_NETWORK_STATE permission not granted in the manifest
        return PlayHaven.ConnectionType.NO_PERMISSION;
    }

    return PlayHaven.ConnectionType.NO_NETWORK;
}

From source file:org.ubicompforall.cityexplorer.CityExplorer.java

public static boolean ensureConnected(Context myContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) myContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo = connectivityManager.getActiveNetworkInfo();
    }/*from  w  w w . j  av a2  s  .  c  om*/
    if (networkInfo == null) {
        return false; //Network is not enabled
    } else {

        boolean activated = networkInfo.getState() == NetworkInfo.State.CONNECTED ? true : false;
        /**         if ( activated ){
                    //Ping Google
                    activated = verifyGoogleConnection ( context );
                 }
        */
        //Toast.makeText( context, "Network state is "+networkInfo.getState(), Toast.LENGTH_LONG).show();
        return activated;
    }
}