Android: How to determine Network speed in android programmatically
Determining your Network Speed - (Slow Internet Speed)
Using NetworkInfo class, ConnectivityManager and TelephonyManager to determine your Network Type.
Download any file from the internet & calculate how long it took vs number of bytes in the file. ( Only possible way to determine Speed Check )
I have tried the below Logic for my projects, You have also look into this, Hope it helps you.
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
NetworkCapabilities nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
int downSpeed = nc.getLinkDownstreamBandwidthKbps();
int upSpeed = nc.getLinkUpstreamBandwidthKbps();
UPDATE - DEPRECATED LIBRARY - no longer mainteined
original post:
You can use Network Connection Class by Facebook.
Its ConnectionClassStateChangeListener has a method called onBandwidthStateChange
that returns an instance of ConnectionQuality class:
public interface ConnectionClassStateChangeListener {
public void onBandwidthStateChange(ConnectionQuality bandwidthState);
}
ConnectionQuality class is an enum class defined as below:
public enum ConnectionQuality {
/**
* Bandwidth under 150 kbps.
*/
POOR,
/**
* Bandwidth between 150 and 550 kbps.
*/
MODERATE,
/**
* Bandwidth between 550 and 2000 kbps.
*/
GOOD,
/**
* EXCELLENT - Bandwidth over 2000 kbps.
*/
EXCELLENT,
/**
* Placeholder for unknown bandwidth. This is the initial value and will stay at this value
* if a bandwidth cannot be accurately found.
*/
UNKNOWN
}
Check internet speed for mobile network to use this code
ConnectivityManager connectivityManager = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
NetworkCapabilities nc = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
var downSpeed = nc.getLinkDownstreamBandwidthKbps();
var upSpeed = nc.getLinkUpstreamBandwidthKbps();
If check internet speed for wifi network to use this code
public int getWifiLevel()
{
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
int level = WifiManager.calculateSignalLevel(linkSpeed, 5);
return level;
}
And more details refer this link
https://android.jlelse.eu/designing-android-apps-to-handle-slow-network-speed-dedc04119aac
I hope this can help you!
Thank You.
Requires minimum api:21. I use ConnectivityManager
combined with NetworkCapabilities
to get both the downstream and upstream bandwidth. Works just fine. You can decide whether speed is 2G,3G or 4G level based on the speed in kbps.
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
NetworkCapabilities nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
var downSpeed = nc.getLinkDownstreamBandwidthKbps();
var upSpeed = nc.getLinkUpstreamBandwidthKbps();
- 2G GSM ~14.4 Kbps
- G GPRS ~26.8 Kbps
- E EDGE ~108.8 Kbps
- 3G UMTS ~128 Kbps
- H HSPA ~3.6 Mbps
- H+ HSPA+ ~14.4 Mbps-23.0 Mbps
- 4G LTE ~50 Mbps
- 4G LTE-A ~500 Mbps
Downloading a file is over-engineering the solution because after all, you aren't responsible for the users internet connection - they are (or rather their service provider is)! Giving them info based on their current status is more than enough for most use cases. This gives you a speedy way of checking current link speed before performing operations.