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?
我在我的应用程序上有一个显示网络类型(2G或3G或4G)的指示器,但是在获得了网络类型之后,我如何知道它应该在什么速度类别中?
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.
我认为LTE是4G的,但是哪一种才是真正的3G呢?还有什么我认为是2G的吗?
So where do you draw the line between 3G or not 3G?
那么,3G和3G之间的界限在哪里呢?
Update: I found another relevant answer at https://*.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.
更新:我在https://*.com/a/8548926/949577上找到了另一个相关的答案,它使用ConnectivityManager()获取类型和子类型,然后将子类型划分为快还是慢。我不知道是否使用ConnectivityManager()是一个更好的方法,然后使用TelephonyManager(),因为它们都能够返回网络类型。
Also I found a link that compares wireless data standards at http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards.
我还在http://en.wikipedia.org/wiki/比较on_of_wireless_data_standards找到了一个比较无线数据标准的链接。
5 个解决方案
#1
92
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";
}
}
Thanks to assistance from the Android source code. =]
感谢来自Android源代码的帮助。=)
#2
69
Based on the Android Developer document & Wikipedia link here i have given comments & define network type.Check the links in comment.
基于Android开发者文档和*链接,我给出了评论和定义网络类型。检查注释中的链接。
You can use getNetworkType
to get Network type.
您可以使用getNetworkType来获取网络类型。
public class CommonUtils {
/**
* To get device consuming netowkr type is 2g,3g,4g
*
* @param context
* @return "2g","3g","4g" as a String based on the network type
*/
public static String getNetworkType(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:
/**
From this link https://en.wikipedia.org/wiki/Evolution-Data_Optimized ..NETWORK_TYPE_EVDO_0 & NETWORK_TYPE_EVDO_A
EV-DO is an evolution of the CDMA2000 (IS-2000) standard that supports high data rates.
Where CDMA2000 https://en.wikipedia.org/wiki/CDMA2000 .CDMA2000 is a family of 3G[1] mobile technology standards for sending voice,
data, and signaling data between mobile phones and cell sites.
*/
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:
//Log.d("Type", "3g");
//For 3g HSDPA , HSPAP(HSPA+) are main networktype which are under 3g Network
//But from other constants also it will 3g like HSPA,HSDPA etc which are in 3g case.
//Some cases are added after testing(real) in device with 3g enable data
//and speed also matters to decide 3g network type
//https://en.wikipedia.org/wiki/4G#Data_rate_comparison
return "3g";
case TelephonyManager.NETWORK_TYPE_LTE:
//No specification for the 4g but from wiki
//I found(LTE (Long-Term Evolution, commonly marketed as 4G LTE))
//https://en.wikipedia.org/wiki/LTE_(telecommunication)
return "4g";
default:
return "Notfound";
}
}
/**
* To check device has internet
*
* @param context
* @return boolean as per status
*/
public static boolean isNetworkConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnected();
}
}
#3
9
You can use getSubtype()
for more details.
您可以使用getSubtype()来获取更多细节。
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
return info.isConnected();
} else {
return false;
}
#4
3
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.
从技术上讲,1xRTT是一种3G技术(尽管许多人认为它的2G仅仅基于数据速度)。您还需要将WiMax添加到您的switch语句以返回4G。它不再经常使用,但是Sprint的WiMax网络目前仍在运行。
#5
1
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.
我认为你必须硬编码你想要的等值值。对这些技术的快速搜索,可以让你对3G或4G的概念有一定的了解(尽管技术上来说,它们都不是真正的4G)。由于HSPA和HSPA+之间似乎没有区别,所以您可能需要运行某种速度或延迟检查,并以这种方式进行检查。
#1
92
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";
}
}
Thanks to assistance from the Android source code. =]
感谢来自Android源代码的帮助。=)
#2
69
Based on the Android Developer document & Wikipedia link here i have given comments & define network type.Check the links in comment.
基于Android开发者文档和*链接,我给出了评论和定义网络类型。检查注释中的链接。
You can use getNetworkType
to get Network type.
您可以使用getNetworkType来获取网络类型。
public class CommonUtils {
/**
* To get device consuming netowkr type is 2g,3g,4g
*
* @param context
* @return "2g","3g","4g" as a String based on the network type
*/
public static String getNetworkType(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:
/**
From this link https://en.wikipedia.org/wiki/Evolution-Data_Optimized ..NETWORK_TYPE_EVDO_0 & NETWORK_TYPE_EVDO_A
EV-DO is an evolution of the CDMA2000 (IS-2000) standard that supports high data rates.
Where CDMA2000 https://en.wikipedia.org/wiki/CDMA2000 .CDMA2000 is a family of 3G[1] mobile technology standards for sending voice,
data, and signaling data between mobile phones and cell sites.
*/
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:
//Log.d("Type", "3g");
//For 3g HSDPA , HSPAP(HSPA+) are main networktype which are under 3g Network
//But from other constants also it will 3g like HSPA,HSDPA etc which are in 3g case.
//Some cases are added after testing(real) in device with 3g enable data
//and speed also matters to decide 3g network type
//https://en.wikipedia.org/wiki/4G#Data_rate_comparison
return "3g";
case TelephonyManager.NETWORK_TYPE_LTE:
//No specification for the 4g but from wiki
//I found(LTE (Long-Term Evolution, commonly marketed as 4G LTE))
//https://en.wikipedia.org/wiki/LTE_(telecommunication)
return "4g";
default:
return "Notfound";
}
}
/**
* To check device has internet
*
* @param context
* @return boolean as per status
*/
public static boolean isNetworkConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnected();
}
}
#3
9
You can use getSubtype()
for more details.
您可以使用getSubtype()来获取更多细节。
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
return info.isConnected();
} else {
return false;
}
#4
3
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.
从技术上讲,1xRTT是一种3G技术(尽管许多人认为它的2G仅仅基于数据速度)。您还需要将WiMax添加到您的switch语句以返回4G。它不再经常使用,但是Sprint的WiMax网络目前仍在运行。
#5
1
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.
我认为你必须硬编码你想要的等值值。对这些技术的快速搜索,可以让你对3G或4G的概念有一定的了解(尽管技术上来说,它们都不是真正的4G)。由于HSPA和HSPA+之间似乎没有区别,所以您可能需要运行某种速度或延迟检查,并以这种方式进行检查。