zoukankan      html  css  js  c++  java
  • android中,获取网速的方法实现

     private long lastTotalRxBytes = 0;
     private long lastTimeStamp = 0;

     /**
         * 得到网络速度
         * @param context
         * @return
         */
        public String getNetSpeed(Context context) {
            String netSpeed = "0 kb/s";
            long nowTotalRxBytes = TrafficStats.getUidRxBytes(context.getApplicationInfo().uid)==TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB;
            long nowTimeStamp = System.currentTimeMillis();
            long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换

            lastTimeStamp = nowTimeStamp;
            lastTotalRxBytes = nowTotalRxBytes;
            netSpeed  = String.valueOf(speed) + " kb/s";
            return  netSpeed;
        }

       在我们主线程中每隔两秒调用一次

  • 相关阅读:
    liunx下手动安装git及配置
    Liunx系统下删除自带的JDK及安装需要的JDK版本
    Pipeline简单实现的代码
    HttpClient-post请求,含图片
    Java
    函数及BOM
    JS--EcmaScript
    定位
    浮动
    盒子模型
  • 原文地址:https://www.cnblogs.com/chengxuxia/p/6744668.html
Copyright © 2011-2022 走看看