Android: How to determine Network speed in android programmatically

前端 未结 8 1094

How to show a slow internet connection to the user when the network is connected Note: Not a network type (2G,3G,4G, WIFI)

8条回答
  •  长情又很酷
    2020-12-31 09:37

    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.

提交回复
热议问题