@cxm-2016
2016-09-22T15:43:31.000000Z
字数 7606
阅读 3123
android
转载:Android 判断用户2G/3G/4G移动数据网络
通过 Android 提供的 TelephonyManager 和 ConnectivityManager 都可以获取到 NetworksInfo 对象,可以通过 getType() 获取类型,判断是 wifi 还是 mobile ,如果是 mobile ,可以通过 NetworksInfo 对象的 getSubType() 和 getSubTypeName() 可以获取到对于的网络类型和名字。
网络类型和名字定义在 TelephonyManager 类里。
/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
TelephonyManager 还提供了 getNetworkTypeName(int type) 的方法,这个方法可以返回一个字符串,但是信息量不大。
那怎么判断是 2G , 3G 还是 4G 网络呢?TelephonyManager 还提供了另外一个方法,getNetworkClass(int networkType) ,但这个方法被隐藏掉了,我把代码贴一下。
fun getNetworkClass(int networkType): Int {
return when (networkType) {
NETWORK_TYPE_GPRS,
NETWORK_TYPE_EDGE,
NETWORK_TYPE_CDMA,
NETWORK_TYPE_1xRTT,
NETWORK_TYPE_IDEN-> {NETWORK_CLASS_2_G}
NETWORK_TYPE_UMTS,
NETWORK_TYPE_EVDO_0,
NETWORK_TYPE_EVDO_A,
NETWORK_TYPE_HSDPA,
NETWORK_TYPE_HSUPA,
NETWORK_TYPE_HSPA,
NETWORK_TYPE_EVDO_B,
NETWORK_TYPE_EHRPD,
NETWORK_TYPE_HSPAP-> {NETWORK_CLASS_3_G}
NETWORK_TYPE_LTE-> {NETWORK_CLASS_4_G}
else-> { NETWORK_CLASS_UNKNOWN}
}
}
然后下面是这几个常量的值。
/** Unknown network class. {@hide} */
public static final int NETWORK_CLASS_UNKNOWN = 0;
/** Class of broadly defined "2G" networks. {@hide} */
public static final int NETWORK_CLASS_2_G = 1;
/** Class of broadly defined "3G" networks. {@hide} */
public static final int NETWORK_CLASS_3_G = 2;
/** Class of broadly defined "4G" networks. {@hide} */
public static final int NETWORK_CLASS_4_G = 3;
最后贴上收集整理的一些资料,可以参考一下。
GPRS 2G(2.5) General Packet Radia Service 114kbps
EDGE 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
UMTS 3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
CDMA 2G 电信 Code Division Multiple Access 码分多址
EVDO_0 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
EVDO_A 3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
1xRTT 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
HSDPA 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
HSUPA 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
HSPA 3G (分HSDPA,HSUPA) High Speed Packet Access
IDEN 2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
EVDO_B 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
LTE 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G
EHRPD 3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
HSPAP 3G HSPAP 比 HSDPA 快些
转自:http://www.binkery.com/post/368.html
实例:
package com.cxm.utils
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import android.telephony.TelephonyManager
import android.util.Log
import java.io.BufferedReader
import java.io.InputStreamReader
/**
* 网络状态工具类
* 陈小默 16/9/22.
*/
class NetUtils(val context: Context) {
/**
* 当前WIFI是否可用
*/
val WIFI_AVAILABLE: Boolean
get() {
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val info = connManager.activeNetworkInfo
return (info != null && info.isConnected && info.type == ConnectivityManager.TYPE_WIFI)
}
/**
* 当前网络是否可用
*/
val NETWORK_ENABLE: Boolean
get() {
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val info = connManager.activeNetworkInfo
return info.state == NetworkInfo.State.CONNECTED
}
/**
* 手机管理器
*/
val TELEPHONY_MANAGER = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
/**
* WiFi管理器
*/
val WIFI_MANAGER = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
/**
* 连接活动管理器
*/
val CONNECTIVITY_MANAGER = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
/**
* WiFi网卡的Mac地址
*/
val WIFI_MAC_ADDRESS: String?
get() {
val info = WIFI_MANAGER.connectionInfo
if (WIFI_MANAGER.isWifiEnabled) {
val localMac = info.macAddress
if (localMac != null) return localMac.toUpperCase()
}
return null
}
/**
* 通过callCmd("busybox ifconfig","HWaddr")获取mac地址
*
* @attention 需要设备装有busybox工具
*
* @return Mac Address
*/
val MAC_FROM_CALL_CMD: String?
get() {
val result = callCmd("busybox ifconfig", "HWaddr")
if (result == null || result.length <= 0) {
return null
}
// 对该行数据进行解析
// 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67
if (result.length > 0 && result.contains("HWaddr") == true) {
val Mac = result.substring(result.indexOf("HWaddr") + 6,
result.length - 1)
if (Mac.length > 1) {
return Mac.replace(" ".toRegex(), "")
}
}
return result
}
/**
* CMD调用
*/
private fun callCmd(cmd: String, filter: String): String? {
val pro = Runtime.getRuntime().exec(cmd)
val br = BufferedReader(InputStreamReader(pro.inputStream))
// 执行命令cmd,只取结果中含有filter的这一行
var line: String? = ""
do {
if (line!!.contains(filter)) break
line = br.readLine()
} while (line != null)
return line
}
/**
* 本机Mac地址
*/
val MAC_ADDRESS: String?
get() {
return if (WIFI_AVAILABLE) {
WIFI_MAC_ADDRESS
} else {
try {
MAC_FROM_CALL_CMD
} catch (e: Throwable) {
Log.e("Mac-CMD", e.message)
null
}
}
}
/**
* 中国运营商的名字
*/
val PROVIDER: String
get() {
val IMSI = TELEPHONY_MANAGER.subscriberId
if (IMSI == null) {
if (TelephonyManager.SIM_STATE_READY == TELEPHONY_MANAGER.simState) {
val operator = TELEPHONY_MANAGER.simOperator
if (operator != null) {
if (operator.equals("464000") || operator.equals("464002") || operator.equals("464007")) {
return "中国移动"
} else if (operator.equals("464001")) {
return "中国联通"
} else if (operator.equals("464003")) {
return "中国电信"
}
}
}
} else {
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")
|| IMSI.startsWith("46007")) {
return "中国移动"
} else if (IMSI.startsWith("46001")) {
return "中国联通"
} else if (IMSI.startsWith("46003")) {
return "中国电信"
}
}
return "UnKnown"
}
/**
* 当前网络的连接类型
*/
val NET_WORK_CLASS: Int?
get() {
val netWorkInfo = CONNECTIVITY_MANAGER.activeNetworkInfo
if (netWorkInfo != null && netWorkInfo.isAvailable && netWorkInfo.isConnected) {
return netWorkInfo.type
} else {
return null
}
}
/**
* 当前网络的连接类型
*/
val NET_WORK_TYPE: NetworkType
get() {
if (WIFI_AVAILABLE) return NetworkType.WIFI
return when (NET_WORK_CLASS) {
TelephonyManager.NETWORK_TYPE_GPRS,
TelephonyManager.NETWORK_TYPE_EDGE,
TelephonyManager.NETWORK_TYPE_CDMA,
TelephonyManager.NETWORK_TYPE_1xRTT,
TelephonyManager.NETWORK_TYPE_IDEN -> {
NetworkType.DATA2G
}
TelephonyManager.NETWORK_TYPE_UMTS,
TelephonyManager.NETWORK_TYPE_EVDO_0,
TelephonyManager.NETWORK_TYPE_EVDO_A,
TelephonyManager.NETWORK_TYPE_HSDPA,
TelephonyManager.NETWORK_TYPE_HSUPA,
TelephonyManager.NETWORK_TYPE_HSPA,
TelephonyManager.NETWORK_TYPE_EVDO_B,
TelephonyManager.NETWORK_TYPE_EHRPD,
TelephonyManager.NETWORK_TYPE_HSPAP -> {
NetworkType.DATA3G
}
TelephonyManager.NETWORK_TYPE_LTE -> {
NetworkType.DATA4G
}
else -> {
NetworkType.UNKNOWN
}
}
}
/**
* 当前WiFi信号强度,单位"dBm"
*/
val WIFI_RSSI: Int
get() {
if (NET_WORK_TYPE == NetworkType.WIFI) {
val wifiInfo = WIFI_MANAGER.connectionInfo
return if (wifiInfo == null) 0 else wifiInfo.rssi
}
return 0
}
/**
* 获得连接WiFi的名称
*/
val WIFI_SSID: String?
get() {
if (NET_WORK_TYPE == NetworkType.WIFI) {
val wifiInfo = WIFI_MANAGER.connectionInfo
return wifiInfo?.ssid
}
return null
}
/**
* 当前SIM卡状态
*/
val SIM_STATE: Boolean
= !(TELEPHONY_MANAGER.simState == TelephonyManager.SIM_STATE_ABSENT
|| TELEPHONY_MANAGER.simState == TelephonyManager.SIM_STATE_UNKNOWN)
/**
* 手机串号
*/
val IMEI: String? = TELEPHONY_MANAGER.deviceId
/**
* 国际移动用户标识码
*/
val IMSI: String? = TELEPHONY_MANAGER.subscriberId
}
enum class NetworkType {
UNKNOWN,
DISCONNECTION,
WIFI,
DATA2G,
DATA3G,
DATA4G,
DATA5G
}