@EActivity(R.layout.m_main) public class TrafficStats_Test extends Activity { @ViewById(R.id.tvsadaas) TextView textView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); timer =new Timer(); // setContentView(); // TrafficStats. } Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { textView.setText((CharSequence) msg.obj); } }; Timer timer; @AfterViews public void exeAciton(){ timer.schedule(task,1000,1000); // timer.s } private void showNetSpeed() { long nowTotalRxBytes = getTotalRxBytes1(); long nowTimeStamp = System.currentTimeMillis(); long speed = nowTotalRxBytes - lastTotalRxBytes; // long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换 lastTimeStamp = nowTimeStamp; lastTotalRxBytes = nowTotalRxBytes; Message msg = mHandler.obtainMessage(); msg.what = 100; msg.obj = String.valueOf(speed) + " kB/s"; mHandler.sendMessage(msg);//更新界面 } private long lastTotalRxBytes = 0; private long lastTimeStamp = 0; private long getTotalRxBytes() { //获取APP的接受速率 Logshow.logshow(TrafficStats.getUidRxBytes(getApplicationInfo().uid)+"time"); return TrafficStats.getUidRxBytes(getApplicationInfo().uid)==TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB } private long getTotalRxBytes1() { //获取手机下载速度 Logshow.logshow(TrafficStats.getTotalRxBytes()+"time"); return TrafficStats.getTotalRxBytes();//转为KB } TimerTask task = new TimerTask() { @Override public void run() { showNetSpeed(); // timer. // timer.schedule(task,1000); } }; } 这里用了androidannotations 注解框架
android TrafficStats 下载,上行速率
最新推荐文章于 2021-05-27 15:31:19 发布