get Network Connect Type - Android android.net

Android examples for android.net:ConnectivityManager

Description

get Network Connect Type

Demo Code

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class Main {

  public static final int NETWORKTYPE_WIFI = 3;

  public static final int NETWORKTYPE_3G = 2;

  public static final int NETWORKTYPE_INVALID = 0;

  public static final int NETWORKTYPE_2G = 1;

  public static int getNetworkConnectType(Context context) {
    int result = NETWORKTYPE_INVALID;
    ConnectivityManager connectivityManager = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (null != networkInfo && networkInfo.isConnected()) {
      int type = networkInfo.getType();
      if (type == ConnectivityManager.TYPE_WIFI) {
        result = NETWORKTYPE_WIFI;//  w w w.j ava 2  s  .  c  o m
      } else if (type == ConnectivityManager.TYPE_MOBILE) {
        int subType = networkInfo.getSubtype();
        switch (subType) {
        case TelephonyManager.NETWORK_TYPE_1xRTT:// ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_CDMA:// ~ 14-64 kbps
        case TelephonyManager.NETWORK_TYPE_EDGE:// ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_IDEN:// ~25 kbps
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
          result = NETWORKTYPE_2G;
          break;
        case TelephonyManager.NETWORK_TYPE_EVDO_0:// ~ 400-1000 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_A:// ~ 600-1400 kbps
        case TelephonyManager.NETWORK_TYPE_HSDPA:// ~ 2-14 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPA: // ~ 700-1700 kbps
        case TelephonyManager.NETWORK_TYPE_HSUPA:// ~ 1-23 Mbps
        case TelephonyManager.NETWORK_TYPE_UMTS:// ~ 400-7000 kbps
        case TelephonyManager.NETWORK_TYPE_EHRPD:// ~ 1-2 Mbps
        case TelephonyManager.NETWORK_TYPE_EVDO_B: // ~ 5 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPAP: // ~ 10-20 Mbps
        case TelephonyManager.NETWORK_TYPE_LTE:// ~ 10+ Mbps
          result = NETWORKTYPE_3G;
          break;
        }
      }
    }
    return result;
  }

}

Related Tutorials