java 如何以编程方式获得链接速度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4429605/
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-10-30 06:19:55  来源:igfitidea点击:

How to get link speed programmatically?

javaandroidnetworking

提问by 4yBAk

I am working on an app and it is almost finished except only one thing: I don't know how to get link speed and place it in the status bar.I am new to Java so if somebody could help me I would be very grateful. P.S. Sorry for bad English.

我正在开发一个应用程序,它几乎完成了,只有一件事:我不知道如何获得链接速度并将其放在状态栏中。我是 Java 新手,所以如果有人可以帮助我,我将非常感激. PS抱歉英语不好。

回答by Fernando Miguélez

As the repliers suggest, your question is not very clear. You could be referring to the link connection speed(i.e up to 54 Mbps with good signal reception Wifi or up to 7.2 Mbps with full speed HSDPA) which depends on:

正如回答者所说,你的问题不是很清楚。您可能指的是链接连接速度(即高达 54 Mbps 且信号接收良好的 Wifi 或高达 7.2 Mbps 的全速 HSDPA),这取决于:

  • The network interface you are using at a time. Some phones allow tethering which means you can have both Wifi and Mobile Data Link (GPRS/3G/HSDPA) active at the same time, or on automatic switch (if your Wifi connection drops your phone will switch to Mobile network automatically if activated).
  • The connection speed negotiated at a time. Depending on signal quality/carrier network configuration (some have max. speed limited)/mobile data contract (monthly bandwith quota exceeded normally means defaulting to GPRS speed).
  • 您一次使用的网络接口。有些手机允许网络共享,这意味着您可以同时启用 Wifi 和移动数据链路 (GPRS/3G/HSDPA),或自动切换(如果您的 Wifi 连接断开,您的手机将在激活后自动切换到移动网络)。
  • 一次协商的连接速度。取决于信号质量/运营商网络配置(有些有最大速度限制)/移动数据合同(超出每月带宽配额通常意味着默认为 GPRS 速度)。

In this case I am afraid there is no standard Java API methods to know it, but the Android API the needed functionality:

在这种情况下,恐怕没有标准的 Java API 方法可以知道它,但是 Android API 所需的功能:

  • For WiFi link speed check WifiInfo.getLinkSpeed()
  • For Mobile Data Link I am afraid that you can only check TelefonyManager.getNetworkType()to determine the current Mobile Data Link type. You should then aproximate to actual speed by link type (i.e. for GPRS up to 128 kbps, for EDGE up to 236.8 kpbs, for 3G up to 2 Mbps, for HSDPA up to 7.2 Mbps). Take into consideration that this is only an aproximation. Your could be conneting using HSDPA but your carrier limiting the top speed to 2 Mbps.
  • 对于 WiFi 链接速度检查WifiInfo.getLinkSpeed()
  • 对于移动数据链路,恐怕您只能检查TelefonyManager.getNetworkType()来确定当前的移动数据链路类型。然后,您应该根据链路类型来近似实际速度(即,GPRS 最高 128 kbps,EDGE 最高 236.8 kpbs,3G 最高 2 Mbps,HSDPA 最高 7.2 Mbps)。考虑到这只是一个近似值。您可以使用 HSDPA 进行连接,但您的运营商将最高速度限制为 2 Mbps。

In the other case that you refer the current (Download/Upload) data link speedthis is only available at a high level, actually measuring not the link speed but the speed between your phone and a server, which can be determinted not only by your link speed but also by many other factors (all the links between your phone an server, the server itself, etc.). You could just measure "HTTP level speed" which means HTTP data speed (leaving out overhead traffic for data packets), since normally only HTTP connections are supported in every scenario (your carrier could be hiding you behind a proxy that filters everything out but HTTP traffic).

另一种情况下,您参考当前(下载/上传)数据链接速度,这仅在较高级别可用,实际上测量的不是链接速度,而是您的手机和服务器之间的速度,这不仅可以由您的链接速度还受许多其他因素的影响(您的手机与服务器之间的所有链接、服务器本身等)。您可以只测量“HTTP 级速度”,这意味着 HTTP 数据速度(忽略数据包的开销流量),因为通常在每种情况下都只支持 HTTP 连接(您的运营商可能将您隐藏在一个代理后面,该代理可以过滤除 HTTP 之外的所有内容)交通)。

If you are using 8 level API an interesting feature called TrafficStatsis also available. This lets you know the sent/received packets at a low level exchanged by your phone over the Mobile Data Link, which may offer just the information you where looking for (use these measurements with elapsed times and you can easily measure current/average used data link speed).

如果您使用的是 8 级 API,则还可以使用一个名为TrafficStats的有趣功能。这让您可以了解手机通过移动数据链路交换的低级别发送/接收数据包,这可能仅提供您正在寻找的信息(使用这些经过时间的测量,您可以轻松测量当前/平均使用数据链接速度)。

回答by Thorbj?rn Ravn Andersen

You cannot tell directly. You must ask the underlying operating system. For OS X you can parse the output from "/sbin/ifconfig" on the appropriate network port.

你不能直接告诉。您必须询问底层操作系统。对于 OS X,您可以在适当的网络端口上解析“/sbin/ifconfig”的输出。

回答by Marcin

You can also write extension using JNI, and ask connection speed using C. It's just in case if you don't want to parse output from other application, but please keep in mind that this solution isn't portable.

您也可以使用 JNI 编写扩展,并使用 C 询问连接速度。以防万一您不想解析其他应用程序的输出,但请记住,此解决方案不可移植。