如何确定是否网络类型为2G,3G或4G [英] How to determine if network type is 2G, 3G or 4G
问题描述
我有我的应用程序来显示网络类型(2G或3G或4G)的指标,但得到的网络类型后,我怎么知道应该在什么速度类别?
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)
我认为LTE是4G,但这些都是真的认为是3G? 别的我会考虑2G。
I would consider LTE to be 4G, but which of these are really considered 3G? Anything else I would consider 2G.
那么,你在哪里画3G或3G没有之间的界限?
So where do you draw the line between 3G or not 3G?
更新: 我发现了另一个相关答案在 http://stackoverflow.com/a/8548926/949577 它采用ConnectivityManager()来获取类型和子类型,然后亚型快速或不分类。 我不知道,如果使用ConnectivityManager()是一个更好的方法,然后使用TelephonyManager(),因为它们都似乎能够返回的网络类型。
Update: I found another relevant answer at http://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.
此外,我发现,进行比较的无线数据标准的<一个链接href="http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards">http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards.
Also I found a link that compares wireless data standards at http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards.
推荐答案
您可以直接在工具类把这个下面的方法:
You can put this following method directly in your Utility class:
public String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
switch (networkType) {
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 "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 TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "3G";
case TelephonyManager.NETWORK_TYPE_LTE:
return "4G";
default:
return "Unknown";
}
}
由于从Android源$ C $ C援助。 =]
Thanks to assistance from the Android source code. =]
这篇关于如何确定是否网络类型为2G,3G或4G的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!