美文网首页
Android获取实时网速

Android获取实时网速

作者: 啤酒小龙虾 | 来源:发表于2018-06-07 18:04 被阅读0次

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()方法即可 实现

相关文章

网友评论

      本文标题:Android获取实时网速

      本文链接:https://www.haomeiwen.com/subject/mbxnsftx.html