I ran into the case that when I had 4G off and connected to certain wifi access point but without capability to send out or receive data, the flag used to check the connectivity of network was set to be true like below.
NetworkInfo ni = context.getActiveNetworkInfo();
boolean flag = ni.isConnected();
In this case, I should obviously drop the wifi and switch on my 4G or in other words turn to use my 4g instead of wifi network.
But How could I check the quality of wifi connectivity?
you can check the speed in the mobile network using this code,
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class Connectivity {
/*
* HACKISH: These constants aren't yet available in my API level (7), but I need to handle these cases if they come up, on newer versions
*/
public static final int NETWORK_TYPE_EHRPD=14; // Level 11
public static final int NETWORK_TYPE_EVDO_B=12; // Level 9
public static final int NETWORK_TYPE_HSPAP=15; // Level 13
public static final int NETWORK_TYPE_IDEN=11; // Level 8
public static final int NETWORK_TYPE_LTE=13; // Level 11
/**
* Check if there is any connectivity
* #param context
* #return
*/
public static boolean isConnected(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return (info != null && info.isConnected());
}
/**
* Check if there is fast connectivity
* #param context
* #return
*/
public static boolean isConnectedFast(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
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){
System.out.println("CONNECTED VIA 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
// NOT AVAILABLE YET IN API LEVEL 7
case Connectivity.NETWORK_TYPE_EHRPD:
return true; // ~ 1-2 Mbps
case Connectivity.NETWORK_TYPE_EVDO_B:
return true; // ~ 5 Mbps
case Connectivity.NETWORK_TYPE_HSPAP:
return true; // ~ 10-20 Mbps
case Connectivity.NETWORK_TYPE_IDEN:
return false; // ~25 kbps
case Connectivity.NETWORK_TYPE_LTE:
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return false;
default:
return false;
}
}else{
return false;
}
}
}
For anyone running into this question, here's an answer from Sep 8, 2015
https://github.com/facebook/network-connection-class
Network Connection Class
Network Connection Class is an Android library that allows you to
figure out the quality of the current user's internet connection. The
connection gets classified into several "Connection Classes" that make
it easy to develop against. The library does this by listening to the
existing internet traffic done by your app and notifying you when the
user's connection quality changes. Developers can then use this
Connection Class information and adjust the application's behaviour
(request lower quality images or video, throttle type-ahead, etc).
Network Connection Class currently only measures the user's downstream
bandwidth. Latency is also an important factor, but in our tests,
we've found that bandwidth is a good proxy for both.
The Network Connection Class library takes care of spikes using a
moving average of the incoming samples, and also applies some
hysteresis (both with a minimum number of samples and amount the
average has to cross a boundary before triggering a bucket change)
Related
My application completely depends on the network so I have to check whether the data communication is there between the application and my server and make a popup on communication lost and if not connected to Wifi or Mobile Data and if the connected service is having slow connection.
I am using the following code where I can get whether the device is connected to the Wifi or mobile data and get the type of connection and also got the code to get the type of mobile network connected which is not working at all.
please refer the following code...
public void CheckInternet()
{
Plugin.Connectivity.CrossConnectivity.Current.ConnectivityChanged += delegate
{
if (!(Plugin.Connectivity.CrossConnectivity.Current.IsConnected))
{
StartActivity(typeof(InternetCheckingView));
}
if (Plugin.Connectivity.CrossConnectivity.Current.IsConnected)
{
ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
NetworkInfo networkInf = connectivityManager.ActiveNetworkInfo;
bool isOnline = networkInf.IsConnected;
NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
bool isWifi = networkInfo.Type == ConnectivityType.Wifi;
bool isdata = networkInfo.Type == ConnectivityType.Mobile;
if (!isOnline)
{ StartActivity(typeof(InternetCheckingView)); }
else if(isWifi)
{
Toast.MakeText(_context, "Wifi Connected", ToastLength.Short).Show();
}
else if (isdata)
{
Toast.MakeText(_context, "Mobile Data Connected", ToastLength.Short).Show();
bool datatype = IsConnectionFast(subType);
if(!(datatype))
{StartActivity(typeof(InternetCheckingView));}
}
}
};
}
public bool IsConnectionFast(object networkType)
{
switch (subType)
{
//case TelephonyManager.NETWORK_TYPE_1xRTT:
case NetworkType.OneXrtt:
return false; // ~ 50-100 kbps//case TelephonyManager.NETWORK_TYPE_CDMA:
case NetworkType.Cdma:
return false; // ~ 14-64 kbps//case TelephonyManager.NETWORK_TYPE_EDGE:
case NetworkType.Edge:
return false; // ~ 50-100 kbps//case TelephonyManager.NETWORK_TYPE_EVDO_0:
case NetworkType.Evdo0:
return true; // ~ 400-1000 kbps //case TelephonyManager.NETWORK_TYPE_EVDO_A:
case NetworkType.EvdoA:
return true; // ~ 600-1400 kbps//case TelephonyManager.NETWORK_TYPE_GPRS:
case NetworkType.Gprs:
return false; // ~ 100 kbps//case TelephonyManager.NETWORK_TYPE_HSDPA:
case NetworkType.Hsdpa:
return true; // ~ 2-14 Mbps//case TelephonyManager.NETWORK_TYPE_HSPA:
case NetworkType.Hspa:
return true; // ~ 700-1700 kbps//case TelephonyManager.NETWORK_TYPE_HSUPA:
case NetworkType.Hsupa:
return true; // ~ 1-23 Mbps//case TelephonyManager.NETWORK_TYPE_UMTS:
case NetworkType.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
case NetworkType.Ehrpd:
return true; // ~ 1-2 Mbps//case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
case NetworkType.EvdoB:
return true; // ~ 5 Mbps//case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
case NetworkType.Hspap:
return true; // ~ 10-20 Mbps//case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
case NetworkType.Iden:
return false; // ~25 kbps//case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
case NetworkType.Lte:
return true; // ~ 10+ Mbps// Unknown//case TelephonyManager.NETWORK_TYPE_UNKNOWN:
case NetworkType.Unknown:
return false;
default:
return false;
}
}
Here I would like to detect the slow network or not and if that is a slow network I have to call StartActivity(typeof(InternetCheckingView))
Can any one please help me to do that in I would like to open a popup whenever there is no data communication available like a onclicklisner in so that the even filers when ever there is a data loss even when the network is connected
(Added--)
I am checking for the data communication by pinging a url when the network connected change like disconnected or changes between wifi and mobile data but i would like to know the solution to get a popup to connect network whenever the data communication is lost even if it is connected to the network only when the app is opened.
It is possibile in Android to intercept switching between 3g and GPRS, LTE, EDGE with a broadcast receiver to call a method when mobile data connection changes?
Attention: the problem is not how can i get informations, but how can i INTERCEPT the EVENT!
Thank you.
Update: I accept answer with NDK ;) because, probably, is not possibile with SDK :(
You need to use ConnectivityManager
The primary responsibilities of this class are to:
Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
Send broadcast intents when network connectivity changes
Attempt to "fail over" to another network when connectivity to a network is lost
Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
Provide an API that allows applications to request and select networks for their data traffic
See training Determining and Monitoring the Connectivity Status
Update:
According answer Emil Davtyan
If the problem is to find whether the phone's network is connected and
fast enough to meet your demands you have to handle all the network
types returned by getSubType().
It took me an hour or two to research and write this class to do just
exactly that, and I thought I would share it with others that might
find it useful.
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 https://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>
Sources for network speeds include wikipedia &
http://3gstore.com/page/78_what_is_evdo_mobile_broadband.html
How to check in Android that my mobile packet data runs out if I am on 3g network?
Sorry if my question is not very clear. Here is my code for checking 3G connection.
public boolean checking3GConnection()
{
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity !=null)
{
NetworkInfo Info3G = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(Info3G!=null)
{
if(Info3G.isConnected())
{
return true;
}
}
}
return false;
}
Actually, when I hit the button(I'm expecting to open the browser to navigate to Google.com) and its calling this method, the problem is because of my 3G plan is run out of data and when I navigated to Google.com, I've seen something like "Out of Credit or Data". I don't want this thing, I'm expecting something that when I hit the button it will check if I still have the 3G data or not before navigate to a website.
You can use the below class to resolve all the connectivity issues in Android
Nice class posted by someone on Gist
Here is a Gist of the class, so you can fork it and edited it.
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 your AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
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.
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
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's getType(). 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.
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.
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
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's getType(). 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.