/** * 根据网络类型区分2g/3g/4g * @param networkType * @return */ privatestaticintgetNetworkClassByType(int networkType) { switch (networkType) { case NETWORK_TYPE_UNAVAILABLE: return NETWORK_CLASS_UNAVAILABLE; case NETWORK_TYPE_WIFI: return NETWORK_CLASS_WIFI; case NETWORK_TYPE_GPRS: case NETWORK_TYPE_EDGE: case NETWORK_TYPE_CDMA: case NETWORK_TYPE_1xRTT: case NETWORK_TYPE_IDEN: return NETWORK_CLASS_2_G case NETWORK_TYPE_UMTS: case NETWORK_TYPE_EVDO_0: case NETWORK_TYPE_EVDO_A: case NETWORK_TYPE_HSDPA: case NETWORK_TYPE_HSUPA: case NETWORK_TYPE_HSPA: case NETWORK_TYPE_EVDO_B: case NETWORK_TYPE_EHRPD: case NETWORK_TYPE_HSPAP: return NETWORK_CLASS_3G case NETWORK_TYPE_LTE: return NETWORK_CLASS_4G; default: return NETWORK_CLASS_UNKNOWN; } }
/** * 获取当前的网络类型 * * @param context * @return */ publicstatic String getCurrentNetworkType(Context context) { int networkClass = getNetworkClass(context); String type = "未知"; switch (networkClass) { case NETWORK_CLASS_UNAVAILABLE: type = "无"; break; case NETWORK_CLASS_WIFI: type = "Wi-Fi"; break; case Constant.NETWORK_CLASS_2G: type = "2G"; break; case Constant.NETWORK_CLASS_3G: type = "3G"; break; case Constant.NETWORK_CLASS_4G: type = "4G"; break; case Constant.NETWORK_CLASS_RESERVE0: type = "未知"; break; } return type; }