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:Main.java

public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (null == cm) {
        return false;
    }/*from   w w w  . j av  a  2 s  .  c  o  m*/

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (null != info && info.isConnected()) {
        if (info.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean checkConnection(Context context) throws NoConnectionPendingException {

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity == null) {
        return false;
    } else {//from  w w  w.ja v  a 2s. co  m

        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        if (info.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }//from   www  . j a v a 2s  .com
    }
    return false;
}

From source file:Main.java

public final static boolean isNetWorkConnected(Context ctx) {
    ConnectivityManager manager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = manager.getActiveNetworkInfo();
    if (network != null && network.isConnected()) {
        if (network.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }/*w w w. java  2s  .  c  o  m*/
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {//  w  w w.j a v  a2 s .c o m
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null && ni.isConnected()) {
                if (ni.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:Main.java

public static NetworkInfo.State getWifiConnectState(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWiFiNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (mWiFiNetworkInfo != null) {
        return mWiFiNetworkInfo.getState();
    }/*from  www.j a  va2  s . c  o  m*/
    return NetworkInfo.State.DISCONNECTED;
}

From source file:Main.java

public static boolean networkAvailable(Context context) {

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity != null) {

        NetworkInfo info = connectivity.getActiveNetworkInfo();

        if (info != null && info.isConnected()) {

            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }/*www  .jav  a2s .c o m*/
        }
    }
    return false;
}

From source file:Main.java

public static boolean checkNet(Context context) {
    try {/* ww  w. jav  a 2s.co m*/
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {//from  www .j  a v  a  2 s  .  c om
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:Main.java

public static String[] getNetworkState(Context pContext) {
    String[] type = new String[2];
    type[0] = "Unknown";
    type[1] = "Unknown";
    if (pContext.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE",
            pContext.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
        ConnectivityManager localConnectivityManager = (ConnectivityManager) pContext
                .getSystemService("connectivity");
        if (localConnectivityManager == null)
            return type;

        NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
        if ((localNetworkInfo1 != null) && (localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED)) {
            type[0] = "Wi-Fi";
            type[1] = localNetworkInfo1.getSubtypeName();
            return type;
        }/*from w w w  .  j av a  2s  .  c om*/
        NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
        if ((localNetworkInfo2 == null) || (localNetworkInfo2.getState() != NetworkInfo.State.CONNECTED))
            type[0] = "2G/3G";
        type[1] = localNetworkInfo2.getSubtypeName();
        return type;
    }
    return type;
}