【问题标题】:Check the bandwidth rate in Android在 Android 中检查带宽速率
【发布时间】:2011-11-22 11:08:23
【问题描述】:

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

我需要检查带宽速率。我需要发起呼叫。为此,我需要检查带宽速率。只有在特定带宽以上,我才需要显示一个呼叫选项(以发起呼叫)。

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

【问题讨论】:

    标签: android bandwidth codec


    【解决方案1】:

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

    样品,未经测试:

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

    【讨论】:

    • 我需要在 Android 中以编程方式查找连接速度
    • 顺便说一句,这也是以编程方式
    • 文件越大,测试越精确,但显然最长。在我看来,一个好的折衷方案是 50KB。
    • 嗯...我不认为下载可能需要 0 毫秒,但是检查一下是明智的
    • @Elmac : 可以查看stackoverflow.com/questions/22932821/…获取下载进度;并查看下载这个 %/size 需要多长时间才能知道当前的下载速度
    【解决方案2】:

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

    但这适用于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
    }
    

    【讨论】:

    • 我需要检查Mobile Data Link(主要是EDGE)的连接速度
    • 另外,此方法仅返回当前关联的 Wi-Fi AP 的理论最大链接速度,对于大多数 AP,通常为 54 mbps。
    • linkSpeed 来自 kbps 还是 mbps?
    【解决方案3】:

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

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

    手动获取代码:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      相关资源
      最近更新 更多