Example usage for android.net NetworkInfo getType

List of usage examples for android.net NetworkInfo getType

Introduction

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

Prototype

@Deprecated
public int getType() 

Source Link

Document

Reports the type of network to which the info in this NetworkInfo pertains.

Usage

From source file:Main.java

/**
 * Get network type/* ww w  .ja v  a  2  s.  c om*/
 *
 * @param context
 * @return boolean
 */
public static int getNetworkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
    return networkInfo == null ? -1 : networkInfo.getType();
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    int netType = NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NETWORK_WIFI;/*from   www .ja  v  a 2s.co m*/
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

            case NETWORK_TYPE_GSM:
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                netType = NETWORK_2G;
                break;

            case NETWORK_TYPE_TD_SCDMA:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                netType = NETWORK_3G;
                break;

            case NETWORK_TYPE_IWLAN:
            case TelephonyManager.NETWORK_TYPE_LTE:
                netType = NETWORK_4G;
                break;
            default:

                String subtypeName = info.getSubtypeName();
                if (subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA")
                        || subtypeName.equalsIgnoreCase("CDMA2000")) {
                    netType = NETWORK_3G;
                } else {
                    netType = NETWORK_UNKNOWN;
                }
                break;
            }
        } else {
            netType = NETWORK_UNKNOWN;
        }
    }
    return netType;
}

From source file:Main.java

public static boolean isWiFiOn(Context context) {
    NetworkInfo activeNetwork = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();//  www. j  a v a2  s . c  o  m
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting()
            && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:Main.java

public static boolean isOnWiFi(Context context) {
    NetworkInfo activeNetwork = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();//from   w  w w  .  j  av a 2  s  .  c  o m

    return activeNetwork != null && activeNetwork.isConnectedOrConnecting()
            && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:Main.java

public static int getNetType(Context context) {
    int netWorkType = NETWORK_CLASS_UNKNOWN;

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        int type = networkInfo.getType();

        if (type == ConnectivityManager.TYPE_WIFI) {
            netWorkType = NETWORK_WIFI;/*w  ww  .jav a2  s  .c  o m*/
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;

            case TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }
        }
    }

    return netWorkType;
}

From source file:Main.java

/**
 * Checks for network connectivity via WiFi or Mobile.
 *
 * @param context Context of application
 * @return A concatenated string with two fields separated by a colon.  The first field is Wifi
 * and second field is Mobile.  If wifi is up, there will be a 'w', otherwise no character.
 * If mobile is up, there will be a 'm', otherwise no character.  If neither is up, then the
 * string will only be the colon./*  w  w  w.j a  va 2  s .co  m*/
 */
public static String getNetworkStatus(Context context) {

    String result = "";

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

    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    boolean wifiConnected;
    boolean mobileConnected;
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    } else {
        wifiConnected = false;
        mobileConnected = false;
    }

    if (wifiConnected)
        result += "w";
    result += ":";
    if (mobileConnected)
        result += "m";
    return result;
}

From source file:Main.java

public static String checkNetType(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    String type = "no_network";
    if (info == null) {
        return type;
    }//from www  .  j  a v  a  2s.  c  o  m
    if (info.getType() == ConnectivityManager.TYPE_WIFI) {
        type = "wifi";
    } else {
        switch (info.getSubtype()) {
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            type = "UNKNOWN";
            break;
        case TelephonyManager.NETWORK_TYPE_GPRS:
            type = "GPRS";
            break;
        case TelephonyManager.NETWORK_TYPE_EDGE:
            type = "EDGE";
            break;
        case TelephonyManager.NETWORK_TYPE_UMTS:
            type = "UMTS";
            break;
        case TelephonyManager.NETWORK_TYPE_CDMA:
            type = "CDMA";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
            type = "EVDO_0";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
            type = "EVDO_A";
            break;
        case TelephonyManager.NETWORK_TYPE_1xRTT:
            type = "1xRTT";
            break;
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            type = "HSDPA";
            break;
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            type = "HSUPA";
            break;
        case TelephonyManager.NETWORK_TYPE_HSPA:
            type = "HSPA";
            break;
        case TelephonyManager.NETWORK_TYPE_IDEN:
            type = "IDEN";
            break;
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
            type = "EVDO_B";
            break;
        case TelephonyManager.NETWORK_TYPE_LTE:
            type = "LTE";
            break;
        case TelephonyManager.NETWORK_TYPE_EHRPD:
            type = "EHRPD";
            break;
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            type = "HSPAP";
            break;
        }
    }
    return type;
}

From source file:Main.java

/**
 * Check mobile network and wireless status
 *///from w w  w. ja  v a2s.com
public static boolean isNetworkAvailableExt(Context paramContext) {
    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
            .getSystemService("connectivity");
    if (localConnectivityManager == null) {
        return false;
    }
    while (true) {
        //
        NetworkInfo localNetworkInfo = localConnectivityManager.getActiveNetworkInfo();
        if ((localNetworkInfo == null) || (localNetworkInfo.getState() != NetworkInfo.State.CONNECTED))
            return false;
        if (localNetworkInfo.getType() == 1) {
            return true;
        }
        if (localNetworkInfo.getType() == 0) {
            return true;
        }
        return true;
    }
}

From source file:com.fanfou.app.opensource.util.NetworkHelper.java

public static final boolean isWifi(final Context context) {
    final ConnectivityManager connec = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo info = connec.getActiveNetworkInfo();
    return (info != null) && (info.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:com.nextgis.metroaccess.MetaDownloader.java

static boolean IsNetworkAvailible(Context c) {
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null) {
        int netType = info.getType();
        if (netType == ConnectivityManager.TYPE_WIFI) {
            return info.isConnected();
        } else if (netType == ConnectivityManager.TYPE_MOBILE && !tm.isNetworkRoaming()) {
            return info.isConnected();
        }//from  w ww. j  a va  2  s. c om
    }
    return false;
}