private long rxtxTotal =0;
private DecimalFormat showFloatFormat =new DecimalFormat("0.00");
public void updateViewData() {
long tempSum = TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
long rxtxLast = tempSum -rxtxTotal;
double totalSpeed= rxtxLast *1000 /2000d;
rxtxTotal = tempSum;
wangsu.setText("当前网速:" + showSpeed(totalSpeed)); //设置显示当前网速
}
private String showSpeed(double speed) {
String speedString;
if (speed >=1048576d) {
speedString =showFloatFormat.format(speed /1048576d) +"MB/s";
}else {
speedString =showFloatFormat.format(speed /1024d) +"KB/s";
}
return speedString;
}
然后开启计时器 轮询访问updateViewData()方法即可 实现
网友评论