解决方法
你使用2.2(Froyo)还是更高版本?
如果是这样,在您的应用程序中,导入流量统计信息,当您的应用程序正在使用互联网时,添加以下内容.
下载上载:
long BeforeTime = System.currentTimeMillis(); long TotalTxBeforeTest = TrafficStats.getTotalTxBytes(); long TotalRxBeforeTest = TrafficStats.getTotalRxBytes(); /* DO WHATEVER NETWORK STUFF YOU NEED TO DO */ long TotalTxAfterTest = TrafficStats.getTotalTxBytes(); long TotalRxAfterTest = TrafficStats.getTotalRxBytes(); long AfterTime = System.currentTimeMillis(); double TimeDifference = AfterTime - BeforeTime; double rxDiff = TotalRxAfterTest - TotalRxBeforeTest; double txDiff = TotalTxAfterTest - TotalTxBeforeTest; if((rxDiff != 0) && (txDiff != 0)) { double rxBPS = (rxDiff / (TimeDifference/1000)); // total rx bytes per second. double txBPS = (txDiff / (TimeDifference/1000)); // total tx bytes per second. testing[0] = String.valueOf(rxBPS) + "B/s. Total rx = " + rxDiff; testing[1] = String.valueOf(txBPS) + "B/s. Total tx = " + txDiff; } else { testing[0] = "No uploaded or downloaded bytes."; }
现在你有测试[0]是你的下载速度(大概),测试[1]是你的上传速度(大致)
当您确实进行网络通信或者时间会偏离您的结果时,请确保您只会调用此代码.
根据延迟,没有什么是伟大的在那里.我写了这是未经测试的,但应该可以正常工作,但可能有更好的解决方案可用.
潜伏:
String host = YOUR_HOST HttpGet request = new HttpGet(host); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters,3000); HttpClient httpClient = new DefaultHttpClient(httpParameters); for(int i=0; i<5; i++) { long BeforeTime = System.currentTimeMillis(); HttpResponse response = httpClient.execute(request); long AfterTime = System.currentTimeMillis(); Long TimeDifference = AfterTime - BeforeTime; time[i] = TimeDifference }
注意:请注意,这不会说明您使用时间的延迟,而是让您了解在特定时间段内您正在使用的网络所遇到的延迟.此外,这是请求和响应时间,而不是像“ping”通常会达到网络时间的请求.