【发布时间】:2011-11-22 11:08:23
【问题描述】:
我们可以选择检查 Android 中的网络连接类型(无论是 3G、edge 还是 gprs)。
我需要检查带宽速率。我需要发起呼叫。为此,我需要检查带宽速率。只有在特定带宽以上,我才需要显示一个呼叫选项(以发起呼叫)。
我需要以编程方式查找连接速度(移动数据链路的连接速度,EDGE)。
【问题讨论】:
我们可以选择检查 Android 中的网络连接类型(无论是 3G、edge 还是 gprs)。
我需要检查带宽速率。我需要发起呼叫。为此,我需要检查带宽速率。只有在特定带宽以上,我才需要显示一个呼叫选项(以发起呼叫)。
我需要以编程方式查找连接速度(移动数据链路的连接速度,EDGE)。
【问题讨论】:
您可以从您的服务器下载一个已知大小的文件,并计算下载它需要多长时间。然后你就有了带宽。简单但有效:)
样品,未经测试:
//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);
【讨论】:
这将返回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
}
【讨论】:
试试 facebook 网络库类(库大小 = 16 KB)
我们可以手动获取/也可以使用网络更改监听器。
手动获取代码:
连接质量 cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();
Github 链接 - https://github.com/facebook/network-connection-class
【讨论】: