How to show a slow internet connection to the user when the network is connected Note: Not a network type (2G,3G,4G, WIFI)
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();
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.