My app will feature live video streams. I would like to know if there is a way to detect if the user has 2G, 3G, or 4G on their devices, and if the which category the current connection belongs to? My question is specifically about Android devices.
-
I see it has very types NETWORK_TYPE_UNKNOWN NETWORK_TYPE_GPRS NETWORK_TYPE_EDGE NETWORK_TYPE_UMTS NETWORK_TYPE_HSDPA NETWORK_TYPE_HSUPA NETWORK_TYPE_HSPA NETWORK_TYPE_CDMA NETWORK_TYPE_EVDO_0 NETWORK_TYPE_EVDO_A NETWORK_TYPE_EVDO_B NETWORK_TYPE_1xRTT NETWORK_TYPE_IDEN NETWORK_TYPE_LTE NETWORK_TYPE_EHRPD NETWORK_TYPE_HSPAP – Android Developer Dec 06 '12 at 00:59
-
2Here's a similar question answered http://stackoverflow.com/questions/9283765/how-to-determine-if-network-type-is-2g-3g-or-4g – Vino Dec 06 '12 at 00:59
-
2duplicated question with answers which can be found in here: http://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android – hungr Dec 06 '12 at 01:01
-
I don't think it was fully resolved, how to use the manager to narrow it down to 2G, 3G or 4G. If I think the user has a 3G when its really 2G that has consequences for the experience I am trying to create. – Android Developer Dec 06 '12 at 01:02
2 Answers
Here is a Gist of the class, so you can fork it and edited it.
package com.emil.android.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Check device's network connectivity and speed
* @author emil http://stackoverflow.com/users/220710/emil
*
*/
public class Connectivity {
/**
* Get the network info
* @param context
* @return
*/
public static NetworkInfo getNetworkInfo(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
/**
* Check if there is any connectivity
* @param context
* @return
*/
public static boolean isConnected(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected());
}
/**
* Check if there is any connectivity to a Wifi network
* @param context
* @param type
* @return
*/
public static boolean isConnectedWifi(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
/**
* Check if there is any connectivity to a mobile network
* @param context
* @param type
* @return
*/
public static boolean isConnectedMobile(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
/**
* Check if there is fast connectivity
* @param context
* @return
*/
public static boolean isConnectedFast(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
}
/**
* Check if the connection is fast
* @param type
* @param subType
* @return
*/
public static boolean isConnectionFast(int type, int subType){
if(type==ConnectivityManager.TYPE_WIFI){
return true;
}else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return true; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return true; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return true; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return false; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return false;
}
}else{
return false;
}
}
}
Also make sure to add this permission to you AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
For all less than or equal to 100kbps speed, the network is considered to be as 2G.
For all greater than 100kbps & less than 1mbps speed, the network is considered to be as 3G.
And for all greater than 1mbps speed, the network is considered to be as 4G.
Sources for network speeds include wikipedia http://en.wikipedia.org/wiki/Comparison_of_wireless_data_standards

- 29,217
- 10
- 88
- 134

- 2,471
- 2
- 19
- 24
-
This class is awesome. I find it useful for me. I wish I could accept it as answer. +15000 – Jatin Malwal Feb 12 '14 at 07:31
-
-
-
Might be worth stating that [Emil Davtyan](https://gist.github.com/emil2k) wrote the class - as your answer is written, it looks like **you** wrote it. – Wai Ha Lee Jan 28 '16 at 13:25
I'm not aware of a way to query the hardware capabilities of the device (which radios are present), but if you're asking how to detect the current type of cellular data connection, look to TelephonyManager.getNetworkType()
, which "returns a constant indicating the radio technology (network type) currently in use on the device for data transmission".
I would consider the values NETWORK_TYPE_LTE
and NETWORK_TYPE_HSPAP
to indicates a 4G connection. Since the line between 3G and 4G is blurry, and since the set of older technologies is effectively fixed (we aren't inventing new 2G networks), a better strategy may be identifying network technologies that are not sufficient and displaying a warning if the connection is using a known slow technology (e.g., EDGE).
Also keep in mind that network technology alone doesn't necessarily equate to a certain connection speed. Even a 4G connection can run at speeds that are insufficient for video streaming depending on many factors, some of which are external to the device (weather, signal strength, device battery level, bandwidth available at the cell tower, etc.)
Other caveats:
You should first check whether the active network connection is a cellular connection. To do this, get
ConnectivityManager.getActiveNetworkInfo()
and examine that object'sgetType()
. This will indicate whether the active network is Wi-Fi or cellular. Keep in mind that there may be no active network (null
will be returned).You should also check
ConnectivityManager.isActiveNetworkMetered()
for a hint about whether the current network connection has a data restriction. If so, you should warn the user before performing data-intensive operations regardless of the connection type.

- 13,885
- 6
- 48
- 73
-
2@AndroidDeveloper You'll have to briefly research the various types of networks to determine the generation and theoretical maximum speed of each; however, I recommend identifying only the network types that are definitely not suitable (2G or earlier) and ruling them out. **You cannot effectively differentiate between a 3G and 4G connection with this approach.** – quietmint Dec 06 '12 at 01:20
-
I am hoping for some more feedback, to see if someone has distinguished the different network types and categorized their speed. This will hopefully save myself and others a lot of time. – Android Developer Dec 06 '12 at 18:55
-
I am using these codes on my apps. I also found it somewhere in the web I just cant remember where. – kuchi Aug 27 '13 at 02:12
-
11xRTT - (50-100 kbps) CDMA - (14-64 kbps) EDGE - (50-100 kbps) EVDO - (400-1000 kbps) EVDO_A - (600-1400 kbps) GPRS - (100 kbps) HSDPA - (2-14 Mbps) HSPA - (700-1700 kbps) HSUPA - (1-23 Mbps) UMTS - (400-7000 kbps) EHRPD - (1-2 Mbps) EVDO_B - (5 Mbps) HSPAP - (10-20 Mbps) IDEN - (25 kbps ) LTE - (10+ Mbps) – kuchi Aug 27 '13 at 02:19
-
@kuchi Remember that network congestion, data plan overages, and other factors mean the connection type is *not* a reliable indicator of actual speed. – quietmint Aug 27 '13 at 20:51