Android中获取实时网速(2)

一、实现思路: 
1、Android提供有获取当前总流量的方法 
2、上一秒 减去 下一面的流量差便是网速 
3、注意计算 
二、计算网速的工具类:

package imcs.cb.com.viewapplication.utils;
 
import android.content.Context;
import android.net.TrafficStats;
import android.os.Handler;
import android.os.Message;
 
import java.util.Timer;
import java.util.TimerTask;
 
/**
 * Created by Ricky on 2016/10/13.
 */
public class NetWorkSpeedUtils {
    private Context context;
    private Handler mHandler;
 
    private long lastTotalRxBytes = 0;
    private long lastTimeStamp = 0;
 
    public NetWorkSpeedUtils(Context context, Handler mHandler){
        this.context = context;
        this.mHandler = mHandler;
    }
 
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            showNetSpeed();
        }
    };
 
    public void startShowNetSpeed(){
        lastTotalRxBytes = getTotalRxBytes();
        lastTimeStamp = System.currentTimeMillis();
        new Timer().schedule(task, 1000, 1000); // 1s后启动任务,每2s执行一次
 
    }
 
    private long getTotalRxBytes() {
        return TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB
    }
 
    private void showNetSpeed() {
        long nowTotalRxBytes = getTotalRxBytes();
        long nowTimeStamp = System.currentTimeMillis();
        long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换
        long speed2 = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 % (nowTimeStamp - lastTimeStamp));//毫秒转换
 
        lastTimeStamp = nowTimeStamp;
        lastTotalRxBytes = nowTotalRxBytes;
 
        Message msg = mHandler.obtainMessage();
        msg.what = 100;
        msg.obj = String.valueOf(speed) + "." + String.valueOf(speed2) + " kb/s";
        mHandler.sendMessage(msg);//更新界面
    }
}

  三、使用:

private Handler mHnadler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    tv_speed.setText("当前网速: " + msg.obj.toString());
                    break;
            }
            super.handleMessage(msg);
        }
    };
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tv_speed = (TextView) findViewById(R.id.tv_speed);
 
        new NetWorkSpeedUtils(this,mHnadler).startShowNetSpeed();
    }

  参考于:https://blog.csdn.net/xueshao110/article/details/82766390

//-------------------------------------------------------

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;
}

作者:一曲烟雨伊人醉
链接:https://www.jianshu.com/p/377ffb0f24ad
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  然后开启计时器 轮询访问updateViewData()方法即可 实现

参考:https://www.jianshu.com/p/377ffb0f24ad

//----------------------------------------------------------------

https://www.csdn.net/gather_27/MtTakg1sNzQ3MC1ibG9n.html

 

 

  1. package imcs.cb.com.viewapplication.utils;
  2.  
     
  3.  
    import android.content.Context;
  4.  
    import android.net.TrafficStats;
  5.  
    import android.os.Handler;
  6.  
    import android.os.Message;
  7.  
     
  8.  
    import java.util.Timer;
  9.  
    import java.util.TimerTask;
  10.  
     
  11.  
    /**
  12.  
    * Created by Ricky on 2016/10/13.
  13.  
    */
  14.  
    public class NetWorkSpeedUtils {
  15.  
    private Context context;
  16.  
    private Handler mHandler;
  17.  
     
  18.  
    private long lastTotalRxBytes = 0;
  19.  
    private long lastTimeStamp = 0;
  20.  
     
  21.  
    public NetWorkSpeedUtils(Context context, Handler mHandler){
  22.  
    this.context = context;
  23.  
    this.mHandler = mHandler;
  24.  
    }
  25.  
     
  26.  
    TimerTask task = new TimerTask() {
  27.  
    @Override
  28.  
    public void run() {
  29.  
    showNetSpeed();
  30.  
    }
  31.  
    };
  32.  
     
  33.  
    public void startShowNetSpeed(){
  34.  
    lastTotalRxBytes = getTotalRxBytes();
  35.  
    lastTimeStamp = System.currentTimeMillis();
  36.  
    new Timer().schedule(task, 1000, 1000); // 1s后启动任务,每2s执行一次
  37.  
     
  38.  
    }
  39.  
     
  40.  
    private long getTotalRxBytes() {
  41.  
    return TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB
  42.  
    }
  43.  
     
  44.  
    private void showNetSpeed() {
  45.  
    long nowTotalRxBytes = getTotalRxBytes();
  46.  
    long nowTimeStamp = System.currentTimeMillis();
  47.  
    long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换
  48.  
    long speed2 = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 % (nowTimeStamp - lastTimeStamp));//毫秒转换
  49.  
     
  50.  
    lastTimeStamp = nowTimeStamp;
  51.  
    lastTotalRxBytes = nowTotalRxBytes;
  52.  
     
  53.  
    Message msg = mHandler.obtainMessage();
  54.  
    msg.what = 100;
  55.  
    msg.obj = String.valueOf(speed) + "." + String.valueOf(speed2) + " kb/s";
  56.  
    mHandler.sendMessage(msg);//更新界面
  57.  
    }
  58.  
    }
posted on 2019-07-31 16:11  巫山老妖  阅读(2541)  评论(0编辑  收藏  举报