Example usage for android.net NetworkInfo isAvailable

List of usage examples for android.net NetworkInfo isAvailable

Introduction

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

Prototype

@Deprecated
public boolean isAvailable() 

Source Link

Document

Indicates whether network connectivity is possible.

Usage

From source file:Main.java

public static int getNetWorkType(Context context) {
    // showLog("getNetWorkType");
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (null == networkInfo || !networkInfo.isAvailable()) {
        return 1;
    }/*from w  w  w .j a va  2 s  . c  o  m*/
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()) {
        return 2;
    }
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()) {
        return 3;
    }
    return 4;
}

From source file:Main.java

public static boolean networkStatusOK(final Context context) {
    boolean netStatus = false;

    try {/* ww w  .  j a  v  a  2s . c om*/
        ConnectivityManager connectManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            if (activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected()) {
                netStatus = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return netStatus;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wiFiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wiFiNetworkInfo != null) {
            return wiFiNetworkInfo.isAvailable();
        }/*w w  w.ja v  a2s . co  m*/
    }
    return false;
}

From source file:Main.java

public static Boolean IsMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfo != null)
            return networkInfo.isAvailable();
    }/*  w ww . j av a 2s .  co  m*/
    return false;
}

From source file:Main.java

public static int getConnectedType(Context context) {
    ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nInfo = cManager.getActiveNetworkInfo();
    if (nInfo != null && nInfo.isAvailable()) {
        int type = nInfo.getType();
        int subType = nInfo.getSubtype();
        switch (type) {
        case ConnectivityManager.TYPE_MOBILE:
            switch (subType) {
            case 1://TelephonyManager.NETWORK_TYPE_GPRS:
            case 2://TelephonyManager.NETWORK_TYPE_EDGE:
            case 4://TelephonyManager.NETWORK_TYPE_CDMA:
            case 7://TelephonyManager.NETWORK_TYPE_1xRTT:
            case 11://TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case 3://TelephonyManager.NETWORK_TYPE_UMTS:
            case 5://TelephonyManager.NETWORK_TYPE_EVDO_0:
            case 6://TelephonyManager.NETWORK_TYPE_EVDO_A:
            case 8://TelephonyManager.NETWORK_TYPE_HSDPA:
            case 9://TelephonyManager.NETWORK_TYPE_HSUPA:
            case 10://TelephonyManager.NETWORK_TYPE_HSPA:
            case 12://TelephonyManager.NETWORK_TYPE_EVDO_B:
            case 14://TelephonyManager.NETWORK_TYPE_EHRPD:
            case 15://TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;
            case 13://TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }//ww  w.  java2s.  com
        case ConnectivityManager.TYPE_WIFI:

            return NETWORK_CLASS_WIFI;
        }
    }
    return NETWORK_CLASS_UNKNOWN;
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mWiFiNetworkInfo != null) {
            return mWiFiNetworkInfo.isAvailable();
        }/* w w w .  j  a v a 2  s .c  om*/
    }
    return false;
}

From source file:Main.java

public static boolean isNetConnected(Context context) {
    boolean ret = false;
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo == null) {
        return ret;
    }/*from  w ww  .j  av  a 2 s . c o m*/
    ret = networkInfo.isAvailable() & networkInfo.isConnected();
    return ret;
}

From source file:Main.java

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isAvailable();
        }/*w w w .j ava2 s  .  c o m*/
    }
    return false;
}

From source file:Main.java

public static boolean isWifiAvailable(Context context) {
    NetworkInfo[] nets = getConnManager(context).getAllNetworkInfo();
    if (nets != null) {
        NetworkInfo[] networkInfos = nets;
        int length = nets.length;

        for (int i = 0; i < length; ++i) {
            NetworkInfo net = networkInfos[i];
            if (net.getType() == 1) {
                return net.isAvailable();
            }//from  ww w. j  a  v  a 2  s.  co  m
        }
    }

    return false;
}

From source file:Main.java

public static boolean isMobileAvailable(Context context) {
    NetworkInfo[] nets = getConnManager(context).getAllNetworkInfo();
    if (nets != null) {
        NetworkInfo[] networkInfos = nets;
        int length = nets.length;

        for (int i = 0; i < length; ++i) {
            NetworkInfo net = networkInfos[i];
            if (net.getType() == 0) {
                return net.isAvailable();
            }/*from w w w.java2 s  .c om*/
        }
    }

    return false;
}