Java Android:如何以编程方式确定 android 中的网络速度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55159487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:10:51  来源:igfitidea点击:

Android: How to determine Network speed in android programmatically

javaandroidkotlin-android-extensions

提问by Mind_Control

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

如何在连接网络时向用户显示互联网连接缓慢 注意:不是网络类型(2G、3G、4G、WIFI)

采纳答案by Lazy Teddy

Determining your Network Speed - (Slow Internet Speed)

确定您的网络速度 - (慢速网速)

Using NetworkInfo class, ConnectivityManager and TelephonyManager to determine your Network Type.

使用 NetworkInfo 类、ConnectivityManager 和 TelephonyManager 来确定您的网络类型。

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 )

从 Internet 下载任何文件并计算所用时间与文件中的字节数。(确定速度检查的唯一可能方法)

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();

回答by jalako

You can't directly check it.

你不能直接检查它。

However, you could either ping a server and see if the response takes very long or doesn't come back at all.

但是,您可以 ping 服务器并查看响应是否需要很长时间或根本不返回。

You could also do a speedtest. In most applications this would not make much sense though, because it's a hog on the user's data.

你也可以做一个速度测试。不过,在大多数应用程序中,这没有多大意义,因为它会占用用户数据。

回答by FonzTech

The most basic thing you can do is to probe an URL whose uptime is almost 100% like, let's say, www.google.com. From the time you start the request, to the time it has completed, you can just measure the amount of data you have downloaded for the request. So you have the data (space) and time spent. Just divide space by time to get network speed. This is somewhat imprecise, because it also depend on the URL you probe.

您可以做的最基本的事情是探测正常运行时间几乎 100% 的 URL,例如,www.google.com. 从您开始请求到完成,您只需测量为请求下载的数据量。所以你有数据(空间)和花费的时间。只需按时间划分空间即可获得网络速度。这有点不精确,因为它还取决于您探测的 URL。

回答by kkarakk

Requires minimum api:21. I use ConnectivityManagercombined with NetworkCapabilitiesto 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.

需要最低 api:21。我ConnectivityManager结合使用NetworkCapabilities来获得下行和上行带宽。工作得很好。您可以根据以 kbps 为单位的速度来决定速度是 2G、3G 还是 4G 级别。

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
  • 2G GSM ~14.4 Kbps
  • G GPRS ~26.8 Kbps
  • E EDGE ~108.8 Kbps
  • 3G UMTS ~128 Kbps
  • 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.

下载文件是对解决方案的过度设计,因为毕竟,您不对用户的互联网连接负责 - 他们是(或者更确切地说是他们的服务提供商)!对于大多数用例来说,根据他们的当前状态向他们提供信息就足够了。这为您提供了一种在执行操作之前检查当前链接速度的快速方法。

回答by Sovandara LENG

It seem that android doesn't allow this directly. You can try to download some file to determine the speed of your internet. Below are the list of the connection qualities:

似乎android不允许直接这样做。您可以尝试下载一些文件来确定您的互联网速度。以下是连接质量列表:

  • POOR // Bandwidth under 150 kbps.
  • MODERATE // Bandwidth between 150 and 550 kbps.
  • GOOD // Bandwidth over 2000 kbps.
  • EXCELLENT // Bandwidth over 2000 kbps.
  • UNKNOWN // connection quality cannot be found.
  • 差 // 带宽低于 150 kbps。
  • 中等 // 带宽在 150 到 550 kbps 之间。
  • 好 // 带宽超过 2000 kbps。
  • 优秀 // 带宽超过 2000 kbps。
  • UNKNOWN // 无法找到连接质量。

You can check detail in this article https://android.jlelse.eu/designing-android-apps-to-handle-slow-network-speed-dedc04119aac

您可以在本文中查看详细信息 https://android.jlelse.eu/designing-android-apps-to-handle-slow-network-speed-dedc04119aac

回答by Hardik Talaviya

Check internet speed for mobile networkto 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 networkto use this code

如果检查wifi 网络的互联网速度以使用此代码

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

https://android.jlelse.eu/designing-android-apps-to-handle-slow-network-speed-dedc04119aac

I hope this can help you!

我希望这可以帮助你!

Thank You.

谢谢你。

回答by Nicola Gallazzi

UPDATE - DEPRECATED LIBRARY - no longer mainteined

更新 - 已弃用的库 - 不再维护

original post:

原帖:

You can use Network Connection Classby Facebook.

您可以使用Facebook 的网络连接类

Its ConnectionClassStateChangeListener has a method called onBandwidthStateChangethat returns an instance of ConnectionQualityclass:

它的 ConnectionClassStateChangeListener 有一个方法被调用onBandwidthStateChange,它返回一个ConnectionQuality类的实例:

public interface ConnectionClassStateChangeListener {
  public void onBandwidthStateChange(ConnectionQuality bandwidthState);
}

ConnectionQuality class is an enum class defined as below:

ConnectionQuality 类是一个枚举类,定义如下:

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
}