Java Code Examples for android.net.NetworkInfo#getSubtype()

The following examples show how to use android.net.NetworkInfo#getSubtype() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommunicationUtils.java    From vocefiscal-android with Apache License 2.0 6 votes vote down vote up
public static boolean isWifiOr3G(Context ctx)
{
	ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo info = cm.getActiveNetworkInfo();
	if(info==null)
		return false;

	int netType = info.getType();
	int netSubtype = info.getSubtype();
	if (netType == ConnectivityManager.TYPE_WIFI) {
		return info.isConnected();
	} else if (netType == ConnectivityManager.TYPE_MOBILE
			&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
			) {
		return info.isConnected();
	} else {
		return false;
	}
}
 
Example 2
Source File: NetworkUtil.java    From RxZhihuDaily with MIT License 5 votes vote down vote up
/**
 * 获取在Mobile网络下的网络类型. 2G,3G,4G
 *
 * @param context
 * @return
 */
public static int getNetworkClass(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                switch (networkInfo.getSubtype()) {
                    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 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;
                }
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return NETWORK_CLASS_WIFI;
            }
        }
    }
    return NETWORK_CLASS_UNKNOWN;
}
 
Example 3
Source File: NetworkObserver.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
public boolean isMobile3GConnected() {
	switch (networkState) {
		case NETWORK_WIFI: {
			return true;
		}
		case NETWORK_MOBILE: {
			synchronized (this) {
				if (System.currentTimeMillis() - last3GChecked >= 2000) {
					boolean is3GAvailable = false;
					NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
					if (networkInfo != null && networkInfo.isConnected()) {
						int type = networkInfo.getType();
						if (type == ConnectivityManager.TYPE_MOBILE ||
								type == ConnectivityManager.TYPE_MOBILE_DUN) {
							switch (networkInfo.getSubtype()) {
								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:
								case TelephonyManager.NETWORK_TYPE_LTE: {
									is3GAvailable = true;
									break;
								}
							}
						}
					}
					last3GAvailable = is3GAvailable;
					last3GChecked = System.currentTimeMillis();
				}
				return last3GAvailable;
			}
		}
	}
	return false;
}
 
Example 4
Source File: Util.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static @C.NetworkType int getMobileNetworkType(NetworkInfo networkInfo) {
  switch (networkInfo.getSubtype()) {
    case TelephonyManager.NETWORK_TYPE_EDGE:
    case TelephonyManager.NETWORK_TYPE_GPRS:
      return C.NETWORK_TYPE_2G;
    case TelephonyManager.NETWORK_TYPE_1xRTT:
    case TelephonyManager.NETWORK_TYPE_CDMA:
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
    case TelephonyManager.NETWORK_TYPE_EVDO_B:
    case TelephonyManager.NETWORK_TYPE_HSDPA:
    case TelephonyManager.NETWORK_TYPE_HSPA:
    case TelephonyManager.NETWORK_TYPE_HSUPA:
    case TelephonyManager.NETWORK_TYPE_IDEN:
    case TelephonyManager.NETWORK_TYPE_UMTS:
    case TelephonyManager.NETWORK_TYPE_EHRPD:
    case TelephonyManager.NETWORK_TYPE_HSPAP:
    case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
      return C.NETWORK_TYPE_3G;
    case TelephonyManager.NETWORK_TYPE_LTE:
      return C.NETWORK_TYPE_4G;
    case TelephonyManager.NETWORK_TYPE_IWLAN:
      return C.NETWORK_TYPE_WIFI;
    case TelephonyManager.NETWORK_TYPE_GSM:
    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
    default: // Future mobile network types.
      return C.NETWORK_TYPE_CELLULAR_UNKNOWN;
  }
}
 
Example 5
Source File: Util.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static @C.NetworkType int getMobileNetworkType(NetworkInfo networkInfo) {
  switch (networkInfo.getSubtype()) {
    case TelephonyManager.NETWORK_TYPE_EDGE:
    case TelephonyManager.NETWORK_TYPE_GPRS:
      return C.NETWORK_TYPE_2G;
    case TelephonyManager.NETWORK_TYPE_1xRTT:
    case TelephonyManager.NETWORK_TYPE_CDMA:
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
    case TelephonyManager.NETWORK_TYPE_EVDO_B:
    case TelephonyManager.NETWORK_TYPE_HSDPA:
    case TelephonyManager.NETWORK_TYPE_HSPA:
    case TelephonyManager.NETWORK_TYPE_HSUPA:
    case TelephonyManager.NETWORK_TYPE_IDEN:
    case TelephonyManager.NETWORK_TYPE_UMTS:
    case TelephonyManager.NETWORK_TYPE_EHRPD:
    case TelephonyManager.NETWORK_TYPE_HSPAP:
    case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
      return C.NETWORK_TYPE_3G;
    case TelephonyManager.NETWORK_TYPE_LTE:
      return C.NETWORK_TYPE_4G;
    case TelephonyManager.NETWORK_TYPE_IWLAN:
      return C.NETWORK_TYPE_WIFI;
    case TelephonyManager.NETWORK_TYPE_GSM:
    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
    default: // Future mobile network types.
      return C.NETWORK_TYPE_CELLULAR_UNKNOWN;
  }
}
 
Example 6
Source File: AppNetworkMgr.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取当前网络的具体类型
 *
 * @param context 上下文
 * @return 当前网络的具体类型。具体类型可参照TelephonyManager中的NETWORK_TYPE_1xRTT、NETWORK_TYPE_CDMA等字段。当前没有网络连接时返回NetworkUtils.NETWORK_TYPE_NO_CONNECTION
 */
public static int getCurrentNetworkSubtype(Context context) {
    NetworkInfo networkInfo
            = ((ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return networkInfo != null
            ? networkInfo.getSubtype()
            : NETWORK_TYPE_NO_CONNECTION;
}
 
Example 7
Source File: AndroidUtil.java    From NewsMe with Apache License 2.0 5 votes vote down vote up
/**
 * 连接的是否是2G网络
 * 
 * @param context
 * @return
 */
public static boolean isNetworkConnectedBy2G(Context context) {
	NetworkInfo networkInfo = ((ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE))
			.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	if (networkInfo != null && networkInfo.isConnected()) {
		int subtype = networkInfo.getSubtype();
		if (subtype == TelephonyManager.NETWORK_TYPE_GPRS
				|| subtype == TelephonyManager.NETWORK_TYPE_EDGE
				|| subtype == TelephonyManager.NETWORK_TYPE_CDMA) {// 移动和联通2G
			return true;
		}
	}
	return false;
}
 
Example 8
Source File: NetworkUtil.java    From Retrofit2RxjavaDemo with Apache License 2.0 5 votes vote down vote up
/**
 * is2G
 * @param context
 * @return boolean
 */
public static boolean is2G(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null
            && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
            || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS || activeNetInfo
            .getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
        return true;
    }
    return false;
}
 
Example 9
Source File: NetworkUtil.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
public static int getNetworkTypeForLink(Context context) {
    try {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null) {
            if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
                return LinkNetWorkType.WIFI;
            } else {
                if (ni.getType() == ConnectivityManager.TYPE_MOBILE) {
                    switch (ni.getSubtype()) {
                        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 LinkNetWorkType._2G;
                        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 12: // TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case 14: // TelephonyManager.NETWORK_TYPE_EHRPD:
                        case 15: // TelephonyManager.NETWORK_TYPE_HSPAP:
                            return LinkNetWorkType._3G;
                        case 13: // TelephonyManager.NETWORK_TYPE_LTE:
                            return LinkNetWorkType._4G;
                        default:
                            return LinkNetWorkType._2G;
                    }
                }
            }
        }
    } catch (Exception e) {
        return LinkNetWorkType.UNKNOWN;
    }
    return LinkNetWorkType.UNKNOWN;
}
 
Example 10
Source File: NetUtil.java    From Readhub with Apache License 2.0 4 votes vote down vote up
/**
 * 判断当前网络类型-1为未知网络0为没有网络连接1网络断开或关闭2为以太网3为WiFi4为2G5为3G6为4G
 */
public static int getNetworkType2(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectMgr.getActiveNetworkInfo();
    if (networkInfo == null) {
        /** 没有任何网络 */
        return 0;
    }
    if (!networkInfo.isConnected()) {
        /** 网络断开或关闭 */
        return 1;
    }
    if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
        /** 以太网网络 */
        return 2;
    } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        /** wifi网络,当激活时,默认情况下,所有的数据流量将使用此连接 */
        return 3;
    } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        /** 移动数据连接,不能与连接共存,如果wifi打开,则自动关闭 */
        switch (networkInfo.getSubtype()) {
            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:
                /** 2G网络 */
                return 4;
            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:
                /** 3G网络 */
                return 5;
            case TelephonyManager.NETWORK_TYPE_LTE:
                /** 4G网络 */
                return 6;
        }
    }
    /** 未知网络 */
    return -1;
}
 
Example 11
Source File: NetworkUtils.java    From CrawlerForReader with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前网络类型
 * <p>需添加权限
 * {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />}</p>
 *
 * @return 网络类型
 * <ul>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_WIFI   } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_4G     } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_3G     } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_2G     } </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_UNKNOWN} </li>
 * <li>{@link NetworkUtils.NetworkType#NETWORK_NO     } </li>
 * </ul>
 */
public static NetworkType getNetworkType() {
    NetworkType netType = NetworkType.NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo();
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NetworkType.NETWORK_WIFI;
        } 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 = NetworkType.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 = NetworkType.NETWORK_3G;
                    break;

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

                    String subtypeName = info.getSubtypeName();
                    if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                            || subtypeName.equalsIgnoreCase("WCDMA")
                            || subtypeName.equalsIgnoreCase("CDMA2000")) {
                        netType = NetworkType.NETWORK_3G;
                    } else {
                        netType = NetworkType.NETWORK_UNKNOWN;
                    }
                    break;
            }
        } else {
            netType = NetworkType.NETWORK_UNKNOWN;
        }
    }
    return netType;
}
 
Example 12
Source File: NetworkUtil.java    From NIM_Android_UIKit with MIT License 4 votes vote down vote up
/**
 * 仅判断Mobile网络的慢速.蓝牙等其他网络不做判断.
 *
 * @param context
 * @return
 */
public static NetworkSpeedMode getNetworkSpeedModeInMobile(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                switch (networkInfo.getSubtype()) {
                    case TelephonyManager.NETWORK_TYPE_IDEN: // ~25 kbps
                        return NetworkSpeedMode.LOW;
                    case TelephonyManager.NETWORK_TYPE_CDMA: // ~ 14-64 kbps
                        return NetworkSpeedMode.LOW;
                    case TelephonyManager.NETWORK_TYPE_1xRTT: // ~ 50-100 kbps
                        return NetworkSpeedMode.LOW;
                    case TelephonyManager.NETWORK_TYPE_EDGE: // ~ 50-100 kbps
                        return NetworkSpeedMode.LOW;
                    case TelephonyManager.NETWORK_TYPE_GPRS: // ~ 100 kbps
                        return NetworkSpeedMode.LOW;

                    case TelephonyManager.NETWORK_TYPE_EVDO_0: // ~ 400-1000
                        // kbps
                        return NetworkSpeedMode.NORMAL;
                    case TelephonyManager.NETWORK_TYPE_EVDO_A: // ~ 600-1400
                        // kbps
                        return NetworkSpeedMode.NORMAL;
                    case TelephonyManager.NETWORK_TYPE_HSPA: // ~ 700-1700 kbps
                        return NetworkSpeedMode.NORMAL;
                    case TelephonyManager.NETWORK_TYPE_UMTS: // ~ 400-7000 kbps
                        return NetworkSpeedMode.NORMAL;
                    case 14: // TelephonyManager.NETWORK_TYPE_EHRPD: // ~ 1-2
                        // Mbps
                        return NetworkSpeedMode.NORMAL;
                    case 12: // TelephonyManager.NETWORK_TYPE_EVDO_B: // ~ 5
                        // Mbps
                        return NetworkSpeedMode.NORMAL;

                    case TelephonyManager.NETWORK_TYPE_HSDPA: // ~ 2-14 Mbps
                        return NetworkSpeedMode.HIGH;
                    case TelephonyManager.NETWORK_TYPE_HSUPA: // ~ 1-23 Mbps
                        return NetworkSpeedMode.HIGH;
                    case 15: // TelephonyManager.NETWORK_TYPE_HSPAP: // ~ 10-20
                        // Mbps
                        return NetworkSpeedMode.HIGH;
                    case 13: // TelephonyManager.NETWORK_TYPE_LTE: // ~ 10+ Mbps
                        return NetworkSpeedMode.HIGH;

                    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                        return NetworkSpeedMode.NORMAL;
                    default:
                        break;
                }
            }
        }
    }
    return NetworkSpeedMode.UNKNOWN;
}
 
Example 13
Source File: NetworkUtils.java    From MyUtil with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前的网络状态
 * @param context 全局context
 * @return
 * 没有网络-NO
 * WIFI网络-WIFI
 * 4G网络-4G
 * 3G网络-3G
 * 2G网络-2G
 * 未知-Unknown
 */
public static String getAPNType(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo == null) {      //无网络
        return "NO";
    }

    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {       //wifi
        return "WIFI";
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            return "4G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_A
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_HSUPA
                || nSubType == TelephonyManager.NETWORK_TYPE_HSPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_B
                || nSubType == TelephonyManager.NETWORK_TYPE_EHRPD
                || nSubType == TelephonyManager.NETWORK_TYPE_HSPAP
                && !telephonyManager.isNetworkRoaming()) {
            return "3G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                || nSubType == TelephonyManager.NETWORK_TYPE_1xRTT
                || nSubType == TelephonyManager.NETWORK_TYPE_IDEN
                && !telephonyManager.isNetworkRoaming()) {
            return "2G";
        } else {
            return "Unknown";
        }
    }

    return "Unknown";
}
 
Example 14
Source File: NetworkUtil.java    From AndroidWallet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 获取当前网络类型
 * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}
 */
@RequiresPermission("android.permission.ACCESS_NETWORK_STATE")
public static NetworkType getNetworkType(Context context) {
    NetworkType netType = NetworkType.NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NetworkType.NETWORK_WIFI;
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

                case TelephonyManager.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 = NetworkType.NETWORK_3G;
                    break;

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

                case TelephonyManager.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 = NetworkType.NETWORK_2G;
                    break;
                default:
                    String subtypeName = info.getSubtypeName();
                    if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                            || subtypeName.equalsIgnoreCase("WCDMA")
                            || subtypeName.equalsIgnoreCase("CDMA2000")) {
                        netType = NetworkType.NETWORK_3G;
                    } else {
                        netType = NetworkType.NETWORK_UNKNOWN;
                    }
                    break;
            }
        } else {
            netType = NetworkType.NETWORK_UNKNOWN;
        }
    }
    return netType;
}
 
Example 15
Source File: NetUtil.java    From likequanmintv with Apache License 2.0 4 votes vote down vote up
/**
     * 检测当的网络(WLAN、3G/2G)状态
     * @param context Context
     * @return true 表示网络可用
     */
    public static void netWorkType(Context context, NetWorkStatusCallBack callback) {
//        if (callback!=null) {
//            callback.strongNetWork();
//            return;
//        }

        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            //网络不可用就算了
            if (info==null||!info.isConnected()){
                if (callback!=null)
                    callback.noNetWork();
                 return;
            }
            int type = info.getType();
            LogUtil.d("type:"+type);
            if (type== ConnectivityManager.TYPE_WIFI){
                if (callback!=null) {
                    callback.strongNetWork();
                }

            }else if (type== ConnectivityManager.TYPE_MOBILE){
                int subtype = info.getSubtype();
                if (subtype<=12){
                    if (callback!=null){
                        callback.thinNetWork();
                    }
                }else if (subtype>12){
                    if (callback!=null){
                        callback.strongNetWork();
                    }
                }
                /*switch (subtype) {
                    case TelephonyManager.NETWORK_TYPE_GPRS: // 2g

                    case TelephonyManager.NETWORK_TYPE_CDMA: // 2g

                    case TelephonyManager.NETWORK_TYPE_EDGE: // 2g

                    case TelephonyManager.NETWORK_TYPE_1xRTT:

                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        if (callback!=null)
                            callback.thinNetWork();
                        break;

                    case TelephonyManager.NETWORK_TYPE_EVDO_A: // 3g

                    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:
                        if (callback!=null)
                            callback.thinNetWork();
                        break;

                    case TelephonyManager.NETWORK_TYPE_LTE:
                    case 19:  //TelephonyManager.NETWORK_TYPE_LTE_CA
                        if (callback!=null)
                            callback.strongNetWork();
                        break;
                }*/

            }else{
                if (callback!=null)
                    callback.strongNetWork();
            }


        }
    }
 
Example 16
Source File: NetworkUtils.java    From BookReader with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前的网络类型(WIFI,2G,3G,4G)
 * <p>需添加权限 {@code <uses-permission android:name="android.permission
 * .ACCESS_NETWORK_STATE"/>}</p>
 *
 * @param context 上下文
 * @return 网络类型
 * <ul>
 * <li>{@link #NETWORK_WIFI   } = 1;</li>
 * <li>{@link #NETWORK_4G     } = 4;</li>
 * <li>{@link #NETWORK_3G     } = 3;</li>
 * <li>{@link #NETWORK_2G     } = 2;</li>
 * <li>{@link #NETWORK_UNKNOWN} = 5;</li>
 * <li>{@link #NETWORK_NO     } = -1;</li>
 * </ul>
 */
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;
        } 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;
}
 
Example 17
Source File: NetworkMonitorAutoDetect.java    From webrtc_android with MIT License 4 votes vote down vote up
/**
 * Returns connection type and status information about |network|.
 * Only callable on Lollipop and newer releases.
 */
@SuppressLint("NewApi")
NetworkState getNetworkState(  Network network) {
  if (network == null || connectivityManager == null) {
    return new NetworkState(false, -1, -1, -1, -1);
  }
  NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
  if (networkInfo == null) {
    Logging.w(TAG, "Couldn't retrieve information from network " + network.toString());
    return new NetworkState(false, -1, -1, -1, -1);
  }
  // The general logic of handling a VPN in this method is as follows. getNetworkInfo will
  // return the info of the network with the same id as in |network| when it is registered via
  // ConnectivityManager.registerNetworkAgent in Android. |networkInfo| may or may not indicate
  // the type TYPE_VPN if |network| is a VPN. To reliably detect the VPN interface, we need to
  // query the network capability as below in the case when networkInfo.getType() is not
  // TYPE_VPN. On the other hand when networkInfo.getType() is TYPE_VPN, the only solution so
  // far to obtain the underlying network information is to query the active network interface.
  // However, the active network interface may not be used for the VPN, for example, if the VPN
  // is restricted to WiFi by the implementation but the WiFi interface is currently turned
  // off and the active interface is the Cell. Using directly the result from
  // getActiveNetworkInfo may thus give the wrong interface information, and one should note
  // that getActiveNetworkInfo would return the default network interface if the VPN does not
  // specify its underlying networks in the implementation. Therefore, we need further compare
  // |network| to the active network. If they are not the same network, we will have to fall
  // back to report an unknown network.

  if (networkInfo.getType() != ConnectivityManager.TYPE_VPN) {
    // Note that getNetworkCapabilities returns null if the network is unknown.
    NetworkCapabilities networkCapabilities =
        connectivityManager.getNetworkCapabilities(network);
    if (networkCapabilities == null
        || !networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
      return getNetworkState(networkInfo);
    }
    // When |network| is in fact a VPN after querying its capability but |networkInfo| is not of
    // type TYPE_VPN, |networkInfo| contains the info for the underlying network, and we return
    // a NetworkState constructed from it.
    return new NetworkState(networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1,
        networkInfo.getType(), networkInfo.getSubtype());
  }

  // When |networkInfo| is of type TYPE_VPN, which implies |network| is a VPN, we return the
  // NetworkState of the active network via getActiveNetworkInfo(), if |network| is the active
  // network that supports the VPN. Otherwise, NetworkState of an unknown network with type -1
  // will be returned.
  //
  // Note that getActiveNetwork and getActiveNetworkInfo return null if no default network is
  // currently active.
  if (networkInfo.getType() == ConnectivityManager.TYPE_VPN) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        && network.equals(connectivityManager.getActiveNetwork())) {
      // If a VPN network is in place, we can find the underlying network type via querying the
      // active network info thanks to
      // https://android.googlesource.com/platform/frameworks/base/+/d6a7980d
      NetworkInfo underlyingActiveNetworkInfo = connectivityManager.getActiveNetworkInfo();
      // We use the NetworkInfo of the underlying network if it is not of TYPE_VPN itself.
      if (underlyingActiveNetworkInfo != null
          && underlyingActiveNetworkInfo.getType() != ConnectivityManager.TYPE_VPN) {
        return new NetworkState(networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1,
            underlyingActiveNetworkInfo.getType(), underlyingActiveNetworkInfo.getSubtype());
      }
    }
    return new NetworkState(
        networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1, -1, -1);
  }

  return getNetworkState(networkInfo);
}
 
Example 18
Source File: NetworkUtils.java    From letv with Apache License 2.0 4 votes vote down vote up
public static void detectNetwork(Context context) {
    try {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        NetworkInfo networkInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
        String networkName = NETWORK_NAME_NO_NETWORK;
        if (networkInfo != null && networkInfo.isAvailable()) {
            networkName = networkInfo.getTypeName();
            switch (networkInfo.getType()) {
                case 0:
                    networkName = networkInfo.getSubtypeName();
                    switch (networkInfo.getSubtype()) {
                        case 1:
                        case 2:
                        case 4:
                        case 7:
                        case 11:
                            sNetworkType = 4;
                            sNetworkName = NETWORK_NAME_2G;
                            break;
                        case 3:
                        case 5:
                        case 6:
                        case 8:
                        case 9:
                        case 10:
                        case 12:
                        case 14:
                        case 15:
                            sNetworkType = 5;
                            sNetworkName = NETWORK_NAME_3G;
                            break;
                        case 13:
                            sNetworkType = 6;
                            sNetworkName = NETWORK_NAME_4G;
                            break;
                        default:
                            if (!NETWORK_NAME_TD_SCDMA.equalsIgnoreCase(networkName) && !NETWORK_NAME_WCDMA.equalsIgnoreCase(networkName) && !NETWORK_NAME_CDMA2000.equalsIgnoreCase(networkName)) {
                                sNetworkType = 2;
                                sNetworkName = NETWORK_NAME_MOBILE;
                                break;
                            }
                            sNetworkType = 5;
                            sNetworkName = NETWORK_NAME_3G;
                            break;
                            break;
                    }
                case 1:
                    sNetworkType = 3;
                    sNetworkName = NETWORK_NAME_WIFI;
                    break;
                case 9:
                    sNetworkType = 1;
                    sNetworkName = NETWORK_NAME_ETHERNET;
                    break;
                default:
                    if (!NETWORK_NAME_PPPOE.equalsIgnoreCase(networkName)) {
                        sNetworkType = 8;
                        sNetworkName = NETWORK_NAME_MORE;
                        break;
                    }
                    sNetworkType = 1;
                    sNetworkName = NETWORK_NAME_ETHERNET;
                    break;
            }
        }
        sNetworkType = 0;
        sNetworkName = NETWORK_NAME_NO_NETWORK;
        LogTool.i(TAG, "detectNetwork. network name(%s), network type(%s), ip(%s)", networkName, Integer.valueOf(sNetworkType), getIP());
    } catch (Exception e) {
        LogTool.e(TAG, "detectNetwork. " + e.toString());
        sNetworkType = -1;
        sNetworkName = NETWORK_NAME_NO_PERMISSION;
        LogTool.i(TAG, "detectNetwork. network name(%s), network type(%s), ip(%s)", sNetworkName, Integer.valueOf(sNetworkType), getIP());
    }
}
 
Example 19
Source File: XNetworkUtils.java    From XFrame with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前网络类型
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
 *
 * @return 网络类型
 * <ul>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_WIFI   } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_4G     } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_3G     } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_2G     } </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_UNKNOWN} </li>
 * <li>{@link XNetworkUtils.NetworkType#NETWORK_NO     } </li>
 * </ul>
 */
public static NetworkType getNetworkType() {
    NetworkType netType = NetworkType.NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo();
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NetworkType.NETWORK_WIFI;
        } 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 = NetworkType.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 = NetworkType.NETWORK_3G;
                    break;

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

                    String subtypeName = info.getSubtypeName();
                    if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                            || subtypeName.equalsIgnoreCase("WCDMA")
                            || subtypeName.equalsIgnoreCase("CDMA2000")) {
                        netType = NetworkType.NETWORK_3G;
                    } else {
                        netType = NetworkType.NETWORK_UNKNOWN;
                    }
                    break;
            }
        } else {
            netType = NetworkType.NETWORK_UNKNOWN;
        }
    }
    return netType;
}
 
Example 20
Source File: XNetworkUtils.java    From XFrame with Apache License 2.0 2 votes vote down vote up
/**
 * 判断网络是否是4G
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean is4G() {
    NetworkInfo info = getActiveNetworkInfo();
    return info != null && info.isAvailable() && info.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE;
}