[关闭]
@cxm-2016 2016-09-22T15:43:31.000000Z 字数 7606 阅读 3086

Android:判断用户2G/3G/4G移动数据网络

android
转载:Android 判断用户2G/3G/4G移动数据网络


通过 Android 提供的 TelephonyManager 和 ConnectivityManager 都可以获取到 NetworksInfo 对象,可以通过 getType() 获取类型,判断是 wifi 还是 mobile ,如果是 mobile ,可以通过 NetworksInfo 对象的 getSubType() 和 getSubTypeName() 可以获取到对于的网络类型和名字。

网络类型和名字定义在 TelephonyManager 类里。

  1. /** Network type is unknown */
  2. public static final int NETWORK_TYPE_UNKNOWN = 0;
  3. /** Current network is GPRS */
  4. public static final int NETWORK_TYPE_GPRS = 1;
  5. /** Current network is EDGE */
  6. public static final int NETWORK_TYPE_EDGE = 2;
  7. /** Current network is UMTS */
  8. public static final int NETWORK_TYPE_UMTS = 3;
  9. /** Current network is CDMA: Either IS95A or IS95B*/
  10. public static final int NETWORK_TYPE_CDMA = 4;
  11. /** Current network is EVDO revision 0*/
  12. public static final int NETWORK_TYPE_EVDO_0 = 5;
  13. /** Current network is EVDO revision A*/
  14. public static final int NETWORK_TYPE_EVDO_A = 6;
  15. /** Current network is 1xRTT*/
  16. public static final int NETWORK_TYPE_1xRTT = 7;
  17. /** Current network is HSDPA */
  18. public static final int NETWORK_TYPE_HSDPA = 8;
  19. /** Current network is HSUPA */
  20. public static final int NETWORK_TYPE_HSUPA = 9;
  21. /** Current network is HSPA */
  22. public static final int NETWORK_TYPE_HSPA = 10;
  23. /** Current network is iDen */
  24. public static final int NETWORK_TYPE_IDEN = 11;
  25. /** Current network is EVDO revision B*/
  26. public static final int NETWORK_TYPE_EVDO_B = 12;
  27. /** Current network is LTE */
  28. public static final int NETWORK_TYPE_LTE = 13;
  29. /** Current network is eHRPD */
  30. public static final int NETWORK_TYPE_EHRPD = 14;
  31. /** Current network is HSPA+ */
  32. public static final int NETWORK_TYPE_HSPAP = 15;

TelephonyManager 还提供了 getNetworkTypeName(int type) 的方法,这个方法可以返回一个字符串,但是信息量不大。

那怎么判断是 2G , 3G 还是 4G 网络呢?TelephonyManager 还提供了另外一个方法,getNetworkClass(int networkType) ,但这个方法被隐藏掉了,我把代码贴一下。

  1. fun getNetworkClass(int networkType): Int {
  2. return when (networkType) {
  3. NETWORK_TYPE_GPRS,
  4. NETWORK_TYPE_EDGE,
  5. NETWORK_TYPE_CDMA,
  6. NETWORK_TYPE_1xRTT,
  7. NETWORK_TYPE_IDEN-> {NETWORK_CLASS_2_G}
  8. NETWORK_TYPE_UMTS,
  9. NETWORK_TYPE_EVDO_0,
  10. NETWORK_TYPE_EVDO_A,
  11. NETWORK_TYPE_HSDPA,
  12. NETWORK_TYPE_HSUPA,
  13. NETWORK_TYPE_HSPA,
  14. NETWORK_TYPE_EVDO_B,
  15. NETWORK_TYPE_EHRPD,
  16. NETWORK_TYPE_HSPAP-> {NETWORK_CLASS_3_G}
  17. NETWORK_TYPE_LTE-> {NETWORK_CLASS_4_G}
  18. else-> { NETWORK_CLASS_UNKNOWN}
  19. }
  20. }

然后下面是这几个常量的值。

  1. /** Unknown network class. {@hide} */
  2. public static final int NETWORK_CLASS_UNKNOWN = 0;
  3. /** Class of broadly defined "2G" networks. {@hide} */
  4. public static final int NETWORK_CLASS_2_G = 1;
  5. /** Class of broadly defined "3G" networks. {@hide} */
  6. public static final int NETWORK_CLASS_3_G = 2;
  7. /** Class of broadly defined "4G" networks. {@hide} */
  8. public static final int NETWORK_CLASS_4_G = 3;

最后贴上收集整理的一些资料,可以参考一下。

  1. GPRS 2G(2.5) General Packet Radia Service 114kbps
  2. EDGE 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
  3. UMTS 3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
  4. CDMA 2G 电信 Code Division Multiple Access 码分多址
  5. EVDO_0 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
  6. EVDO_A 3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
  7. 1xRTT 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
  8. HSDPA 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
  9. HSUPA 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
  10. HSPA 3G (分HSDPA,HSUPA) High Speed Packet Access
  11. IDEN 2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
  12. EVDO_B 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
  13. LTE 4G Long Term Evolution FDD-LTE TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G
  14. EHRPD 3G CDMA2000LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
  15. HSPAP 3G HSPAP HSDPA 快些
  16. 转自:http://www.binkery.com/post/368.html

实例:

  1. package com.cxm.utils
  2. import android.content.Context
  3. import android.net.ConnectivityManager
  4. import android.net.NetworkInfo
  5. import android.net.wifi.WifiManager
  6. import android.telephony.TelephonyManager
  7. import android.util.Log
  8. import java.io.BufferedReader
  9. import java.io.InputStreamReader
  10. /**
  11. * 网络状态工具类
  12. * 陈小默 16/9/22.
  13. */
  14. class NetUtils(val context: Context) {
  15. /**
  16. * 当前WIFI是否可用
  17. */
  18. val WIFI_AVAILABLE: Boolean
  19. get() {
  20. val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
  21. val info = connManager.activeNetworkInfo
  22. return (info != null && info.isConnected && info.type == ConnectivityManager.TYPE_WIFI)
  23. }
  24. /**
  25. * 当前网络是否可用
  26. */
  27. val NETWORK_ENABLE: Boolean
  28. get() {
  29. val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
  30. val info = connManager.activeNetworkInfo
  31. return info.state == NetworkInfo.State.CONNECTED
  32. }
  33. /**
  34. * 手机管理器
  35. */
  36. val TELEPHONY_MANAGER = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
  37. /**
  38. * WiFi管理器
  39. */
  40. val WIFI_MANAGER = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
  41. /**
  42. * 连接活动管理器
  43. */
  44. val CONNECTIVITY_MANAGER = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
  45. /**
  46. * WiFi网卡的Mac地址
  47. */
  48. val WIFI_MAC_ADDRESS: String?
  49. get() {
  50. val info = WIFI_MANAGER.connectionInfo
  51. if (WIFI_MANAGER.isWifiEnabled) {
  52. val localMac = info.macAddress
  53. if (localMac != null) return localMac.toUpperCase()
  54. }
  55. return null
  56. }
  57. /**
  58. * 通过callCmd("busybox ifconfig","HWaddr")获取mac地址
  59. *
  60. * @attention 需要设备装有busybox工具
  61. *
  62. * @return Mac Address
  63. */
  64. val MAC_FROM_CALL_CMD: String?
  65. get() {
  66. val result = callCmd("busybox ifconfig", "HWaddr")
  67. if (result == null || result.length <= 0) {
  68. return null
  69. }
  70. // 对该行数据进行解析
  71. // 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67
  72. if (result.length > 0 && result.contains("HWaddr") == true) {
  73. val Mac = result.substring(result.indexOf("HWaddr") + 6,
  74. result.length - 1)
  75. if (Mac.length > 1) {
  76. return Mac.replace(" ".toRegex(), "")
  77. }
  78. }
  79. return result
  80. }
  81. /**
  82. * CMD调用
  83. */
  84. private fun callCmd(cmd: String, filter: String): String? {
  85. val pro = Runtime.getRuntime().exec(cmd)
  86. val br = BufferedReader(InputStreamReader(pro.inputStream))
  87. // 执行命令cmd,只取结果中含有filter的这一行
  88. var line: String? = ""
  89. do {
  90. if (line!!.contains(filter)) break
  91. line = br.readLine()
  92. } while (line != null)
  93. return line
  94. }
  95. /**
  96. * 本机Mac地址
  97. */
  98. val MAC_ADDRESS: String?
  99. get() {
  100. return if (WIFI_AVAILABLE) {
  101. WIFI_MAC_ADDRESS
  102. } else {
  103. try {
  104. MAC_FROM_CALL_CMD
  105. } catch (e: Throwable) {
  106. Log.e("Mac-CMD", e.message)
  107. null
  108. }
  109. }
  110. }
  111. /**
  112. * 中国运营商的名字
  113. */
  114. val PROVIDER: String
  115. get() {
  116. val IMSI = TELEPHONY_MANAGER.subscriberId
  117. if (IMSI == null) {
  118. if (TelephonyManager.SIM_STATE_READY == TELEPHONY_MANAGER.simState) {
  119. val operator = TELEPHONY_MANAGER.simOperator
  120. if (operator != null) {
  121. if (operator.equals("464000") || operator.equals("464002") || operator.equals("464007")) {
  122. return "中国移动"
  123. } else if (operator.equals("464001")) {
  124. return "中国联通"
  125. } else if (operator.equals("464003")) {
  126. return "中国电信"
  127. }
  128. }
  129. }
  130. } else {
  131. if (IMSI.startsWith("46000") || IMSI.startsWith("46002")
  132. || IMSI.startsWith("46007")) {
  133. return "中国移动"
  134. } else if (IMSI.startsWith("46001")) {
  135. return "中国联通"
  136. } else if (IMSI.startsWith("46003")) {
  137. return "中国电信"
  138. }
  139. }
  140. return "UnKnown"
  141. }
  142. /**
  143. * 当前网络的连接类型
  144. */
  145. val NET_WORK_CLASS: Int?
  146. get() {
  147. val netWorkInfo = CONNECTIVITY_MANAGER.activeNetworkInfo
  148. if (netWorkInfo != null && netWorkInfo.isAvailable && netWorkInfo.isConnected) {
  149. return netWorkInfo.type
  150. } else {
  151. return null
  152. }
  153. }
  154. /**
  155. * 当前网络的连接类型
  156. */
  157. val NET_WORK_TYPE: NetworkType
  158. get() {
  159. if (WIFI_AVAILABLE) return NetworkType.WIFI
  160. return when (NET_WORK_CLASS) {
  161. TelephonyManager.NETWORK_TYPE_GPRS,
  162. TelephonyManager.NETWORK_TYPE_EDGE,
  163. TelephonyManager.NETWORK_TYPE_CDMA,
  164. TelephonyManager.NETWORK_TYPE_1xRTT,
  165. TelephonyManager.NETWORK_TYPE_IDEN -> {
  166. NetworkType.DATA2G
  167. }
  168. TelephonyManager.NETWORK_TYPE_UMTS,
  169. TelephonyManager.NETWORK_TYPE_EVDO_0,
  170. TelephonyManager.NETWORK_TYPE_EVDO_A,
  171. TelephonyManager.NETWORK_TYPE_HSDPA,
  172. TelephonyManager.NETWORK_TYPE_HSUPA,
  173. TelephonyManager.NETWORK_TYPE_HSPA,
  174. TelephonyManager.NETWORK_TYPE_EVDO_B,
  175. TelephonyManager.NETWORK_TYPE_EHRPD,
  176. TelephonyManager.NETWORK_TYPE_HSPAP -> {
  177. NetworkType.DATA3G
  178. }
  179. TelephonyManager.NETWORK_TYPE_LTE -> {
  180. NetworkType.DATA4G
  181. }
  182. else -> {
  183. NetworkType.UNKNOWN
  184. }
  185. }
  186. }
  187. /**
  188. * 当前WiFi信号强度,单位"dBm"
  189. */
  190. val WIFI_RSSI: Int
  191. get() {
  192. if (NET_WORK_TYPE == NetworkType.WIFI) {
  193. val wifiInfo = WIFI_MANAGER.connectionInfo
  194. return if (wifiInfo == null) 0 else wifiInfo.rssi
  195. }
  196. return 0
  197. }
  198. /**
  199. * 获得连接WiFi的名称
  200. */
  201. val WIFI_SSID: String?
  202. get() {
  203. if (NET_WORK_TYPE == NetworkType.WIFI) {
  204. val wifiInfo = WIFI_MANAGER.connectionInfo
  205. return wifiInfo?.ssid
  206. }
  207. return null
  208. }
  209. /**
  210. * 当前SIM卡状态
  211. */
  212. val SIM_STATE: Boolean
  213. = !(TELEPHONY_MANAGER.simState == TelephonyManager.SIM_STATE_ABSENT
  214. || TELEPHONY_MANAGER.simState == TelephonyManager.SIM_STATE_UNKNOWN)
  215. /**
  216. * 手机串号
  217. */
  218. val IMEI: String? = TELEPHONY_MANAGER.deviceId
  219. /**
  220. * 国际移动用户标识码
  221. */
  222. val IMSI: String? = TELEPHONY_MANAGER.subscriberId
  223. }
  224. enum class NetworkType {
  225. UNKNOWN,
  226. DISCONNECTION,
  227. WIFI,
  228. DATA2G,
  229. DATA3G,
  230. DATA4G,
  231. DATA5G
  232. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注