检查 Android 中的带宽速率

发布于 2024-12-17 13:03:16 字数 164 浏览 2 评论 0原文

我们可以选择检查 Android 中的网络连接类型(无论是 3G、edge 还是 gprs)。

我需要检查带宽速率。我需要拨打电话。为此,我需要检查带宽速率。仅在特定带宽之上,我需要使呼叫选项可见(以发起呼叫)。

我需要以编程方式查找连接速度(移动数据链路、EDGE 的连接速度)。

We have an option to check the network connection types in Android (whether it is 3G, edge or gprs).

I need to check the the bandwidth rate. I need to initiate a call. For that I need to check the bandwidth rate. Above a particular bandwidth only I need to make visible an option for a call (to initiate a call).

I need to find the connection speed programmatically (connection speed for Mobile Data Link, EDGE).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜宝宝 2024-12-24 13:03:16

您可以从服务器下载已知大小的文件,并计算下载它需要多长时间。然后你就有了你的带宽。简单但有效:)

示例,未经测试:

//Download your image
long startTime = System.currentTimeMillis();
HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
long endTime = System.currentTimeMillis();

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity;
bufHttpEntity = new BufferedHttpEntity(entity);

//You can re-check the size of your file
final long contentLength = bufHttpEntity.getContentLength();

// Log
Log.d(TAG, "[BENCHMARK] Dowload time :"+(endTime-startTime)+" ms");

// Bandwidth : size(KB)/time(s)
float bandwidth = contentLength / ((endTime-startTime) *1000);

You can download a known-size file from your server, and calculate how long did it take to download it. Then you have your bandwidth. Simple but works :)

Sample, not tested :

//Download your image
long startTime = System.currentTimeMillis();
HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
long endTime = System.currentTimeMillis();

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity;
bufHttpEntity = new BufferedHttpEntity(entity);

//You can re-check the size of your file
final long contentLength = bufHttpEntity.getContentLength();

// Log
Log.d(TAG, "[BENCHMARK] Dowload time :"+(endTime-startTime)+" ms");

// Bandwidth : size(KB)/time(s)
float bandwidth = contentLength / ((endTime-startTime) *1000);
緦唸λ蓇 2024-12-24 13:03:16

这将返回LINK_SPEED_UNITS 中的当前链接速度。

,但这仅适用于WIFI

WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
    Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}

This will Returns the current link speed in LINK_SPEED_UNITS.

but this work for WIFI Only

WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
    Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}
英雄似剑 2024-12-24 13:03:16

尝试 facebook 网络库类(库大小 = 16 KB)

我们可以手动获取/网络更改侦听器也可用。

手动获取代码:

连接质量 cq =
ConnectionClassManager.getInstance().getCurrentBandwidthQuality();

Github 链接 - https://github.com/facebook/network-connection-class

Try facebook network library class (library size = 16 KB)

We can fetch manually / network change listener also available.

Manual fetch code:

ConnectionQuality cq =
ConnectionClassManager.getInstance().getCurrentBandwidthQuality();

Github link - https://github.com/facebook/network-connection-class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文