如何判断网络类型是2G、3G还是4G
我的应用程序上有一个指示器来显示网络类型(2G 或 3G 或 4G),但在获取网络类型后,我如何知道它应该属于哪个速度类别?
我知道如何检测网络类型:
private TelephonyManager telephonyManager;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
CurrentNetworkType = telephonyManager.getNetworkType();
给定可能的返回值:
// public static final int NETWORK_TYPE_1xRTT
// Since: API Level 4
// Current network is 1xRTT
// Constant Value: 7 (0x00000007)
//
// public static final int NETWORK_TYPE_CDMA
// Since: API Level 4
// Current network is CDMA: Either IS95A or IS95B
// Constant Value: 4 (0x00000004)
//
// public static final int NETWORK_TYPE_EDGE
// Since: API Level 1
// Current network is EDGE
// Constant Value: 2 (0x00000002)
//
// public static final int NETWORK_TYPE_EHRPD
// Since: API Level 11
// Current network is eHRPD
// Constant Value: 14 (0x0000000e)
//
// public static final int NETWORK_TYPE_EVDO_0
// Since: API Level 4
// Current network is EVDO revision 0
// Constant Value: 5 (0x00000005)
//
// public static final int NETWORK_TYPE_EVDO_A
// Since: API Level 4
// Current network is EVDO revision A
// Constant Value: 6 (0x00000006)
//
// public static final int NETWORK_TYPE_EVDO_B
// Since: API Level 9
// Current network is EVDO revision B
// Constant Value: 12 (0x0000000c)
//
// public static final int NETWORK_TYPE_GPRS
// Since: API Level 1
// Current network is GPRS
// Constant Value: 1 (0x00000001)
//
// public static final int NETWORK_TYPE_HSDPA
// Since: API Level 5
// Current network is HSDPA
// Constant Value: 8 (0x00000008)
//
// public static final int NETWORK_TYPE_HSPA
// Since: API Level 5
// Current network is HSPA
// Constant Value: 10 (0x0000000a)
//
// public static final int NETWORK_TYPE_HSPAP
// Since: API Level 13
// Current network is HSPA+
// Constant Value: 15 (0x0000000f)
//
// public static final int NETWORK_TYPE_HSUPA
// Since: API Level 5
// Current network is HSUPA
// Constant Value: 9 (0x00000009)
//
// public static final int NETWORK_TYPE_IDEN
// Since: API Level 8
// Current network is iDen
// Constant Value: 11 (0x0000000b)
//
// public static final int NETWORK_TYPE_LTE
// Since: API Level 11
// Current network is LTE
// Constant Value: 13 (0x0000000d)
//
// public static final int NETWORK_TYPE_UMTS
// Since: API Level 1
// Current network is UMTS
// Constant Value: 3 (0x00000003)
//
// public static final int NETWORK_TYPE_UNKNOWN
// Since: API Level 1
// Network type is unknown
// Constant Value: 0 (0x00000000)
我会认为 LTE 是 4G,但其中哪些真正被视为 3G? 其他的话我会考虑2G。
那么,3G 与非 3G 的界限在哪里呢?
更新: 我在 https://stackoverflow.com/a/8548926/949577 找到了另一个相关答案 它使用 ConnectivityManager() 获取类型和子类型,然后将子类型分类为快速或慢速。 我不知道使用 ConnectivityManager() 是否是比使用 TelephonyManager () 更好的方法,因为它们似乎都能够返回网络类型。
我还发现了一个比较无线数据标准的链接 http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards< /a>.
I have an indicator on my application to display the network type (2G or 3G or 4G) but after getting the network type, how do I know what speed category it should be in?
I know how to detect the network type:
private TelephonyManager telephonyManager;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
CurrentNetworkType = telephonyManager.getNetworkType();
Given the possible return values:
// public static final int NETWORK_TYPE_1xRTT
// Since: API Level 4
// Current network is 1xRTT
// Constant Value: 7 (0x00000007)
//
// public static final int NETWORK_TYPE_CDMA
// Since: API Level 4
// Current network is CDMA: Either IS95A or IS95B
// Constant Value: 4 (0x00000004)
//
// public static final int NETWORK_TYPE_EDGE
// Since: API Level 1
// Current network is EDGE
// Constant Value: 2 (0x00000002)
//
// public static final int NETWORK_TYPE_EHRPD
// Since: API Level 11
// Current network is eHRPD
// Constant Value: 14 (0x0000000e)
//
// public static final int NETWORK_TYPE_EVDO_0
// Since: API Level 4
// Current network is EVDO revision 0
// Constant Value: 5 (0x00000005)
//
// public static final int NETWORK_TYPE_EVDO_A
// Since: API Level 4
// Current network is EVDO revision A
// Constant Value: 6 (0x00000006)
//
// public static final int NETWORK_TYPE_EVDO_B
// Since: API Level 9
// Current network is EVDO revision B
// Constant Value: 12 (0x0000000c)
//
// public static final int NETWORK_TYPE_GPRS
// Since: API Level 1
// Current network is GPRS
// Constant Value: 1 (0x00000001)
//
// public static final int NETWORK_TYPE_HSDPA
// Since: API Level 5
// Current network is HSDPA
// Constant Value: 8 (0x00000008)
//
// public static final int NETWORK_TYPE_HSPA
// Since: API Level 5
// Current network is HSPA
// Constant Value: 10 (0x0000000a)
//
// public static final int NETWORK_TYPE_HSPAP
// Since: API Level 13
// Current network is HSPA+
// Constant Value: 15 (0x0000000f)
//
// public static final int NETWORK_TYPE_HSUPA
// Since: API Level 5
// Current network is HSUPA
// Constant Value: 9 (0x00000009)
//
// public static final int NETWORK_TYPE_IDEN
// Since: API Level 8
// Current network is iDen
// Constant Value: 11 (0x0000000b)
//
// public static final int NETWORK_TYPE_LTE
// Since: API Level 11
// Current network is LTE
// Constant Value: 13 (0x0000000d)
//
// public static final int NETWORK_TYPE_UMTS
// Since: API Level 1
// Current network is UMTS
// Constant Value: 3 (0x00000003)
//
// public static final int NETWORK_TYPE_UNKNOWN
// Since: API Level 1
// Network type is unknown
// Constant Value: 0 (0x00000000)
I would consider LTE to be 4G, but which of these are really considered 3G?
Anything else I would consider 2G.
So where do you draw the line between 3G or not 3G?
Update:
I found another relevant answer at https://stackoverflow.com/a/8548926/949577
It uses ConnectivityManager() to get type and subtype and then classifies the subtype as either fast or not.
I don't know if using ConnectivityManager() is a better approach then using TelephonyManager () since they both appear able to return the network type.
Also I found a link that compares wireless data standards at http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将以下方法直接放入您的 Utility 类中:
Kotlin:
Java:
感谢 Android 源代码的帮助。 =]
You can put this following method directly in your Utility class:
Kotlin:
Java:
Thanks to assistance from the Android source code. =]
基于Android开发者文档&维基百科链接在这里我已经给出了评论和;定义网络类型。检查评论中的链接。
您可以使用
getNetworkType
来获取网络类型。Based on the Android Developer document & Wikipedia link here i have given comments & define network type.Check the links in comment.
You can use
getNetworkType
to get Network type.您可以使用
getSubtype()
获取更多详细信息。You can use
getSubtype()
for more details.API 29 / Android Q 的更新版本,在 Kotlin 中支持 5G:
在您的 Activity 中像这样使用它:
Updated version for API 29 / Android Q with 5G support in Kotlin:
Use it like so in your activity:
从技术上讲,1xRTT 是一种 3G 技术(尽管许多人仅根据数据速度将其视为 2G)。此外,您还需要将 WiMax 添加到 switch 语句中以返回 4G。它不再经常使用,但 Sprint 的 WiMax 网络暂时仍在运行。
Technically speaking 1xRTT is a 3G technology (although many consider it 2G based solely on data speed). Also you will want to add WiMax to your switch statement to return 4G. It's not used often anymore, but Sprint's WiMax network is still operational for the time being.
我认为实际上你只需要硬编码你希望它们具有的等效值。快速谷歌搜索一下这些技术中的大多数,应该会让您对哪些技术被视为 3G 或 4G 有一定的了解(尽管从技术上讲,它们都不是真正的 4G)。由于 HSPA 和 HSPA+ 之间似乎没有区别,因此您可能需要运行某种速度或延迟检查,并按照这种方式进行。
I think really you just have to hardcode the equivalent value you want them to have. A quick googling of most of those technologies should give you some manner of an idea as to which is considered 3G or 4G (though technically none of them are real 4G). Since there doesn't seem to be a distinction between HSPA and HSPA+, you might want to run some sort of speed or latency check, and go with it that way.