• Android显示网速的另一种方法


    (´・_・`)今天我又来重复造轮子了,上/下行数据是从/proc/net/dev读取数据而来,而不是用Android的API来实现的。

    话说之前我居然在主线程使用while,导致状态栏出来不了。如果不是大蛋~我如今还是一头迷途的羔羊!!

    信大蛋,得永生!!信大蛋,得永生!!信大蛋,得永生!!信大蛋,得永生!!

    下面贴出代码,你们自己看吧。。。

    package com.michellgaby.traffic;
    
    import android.content.Context;
    import android.os.Handler;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    import java.io.*;
    
    public class Traffic extends TextView {
    
        // 线程等待时间
        private int mDelaytime = 3000;
        private Handler mHandler = new Handler();
    
        private int mLastReceive = 0;
        private int mLastTransmit = 0;
        private int mRxTxRate = 0;
    
        File traffic = new File("/data/.traffic");
    
        // 一个新的线程
        private Runnable task = new Runnable() {
            public void run() {
                if (!traffic.exists() || mLastReceive == getTotalDataBytes(true) && mLastTransmit == getTotalDataBytes(false)){
                    Traffic.this.setText("");
                } else {
                    mRxTxRate = (getTotalDataBytes(true) - mLastReceive) + (getTotalDataBytes(false) - mLastTransmit);
                    mLastReceive = getTotalDataBytes(true);
                    mLastTransmit = getTotalDataBytes(false);
    
                    if (mRxTxRate > 0)
                        Traffic.this.setText(formatSize(mRxTxRate/3));
                    else
                        Traffic.this.setText("");
                }
    
                mHandler.postDelayed(task, mDelaytime);
            }
        };
    
        // 获取网络传输总数据
        private int getTotalDataBytes(boolean Transmit) {
            String readLine;
            String[] DataPart;
            int line = 0;
            int Data = 0;
            try {
                // 使用BufferedReader打开文件
                FileReader fr = new FileReader("/proc/net/dev");
                BufferedReader br = new BufferedReader(fr);
    
                // 按行读取数据并相加
                while((readLine = br.readLine()) != null) {
                    // 跳过文件头两行
                    line++;
                    if (line <= 2) continue;
    
                    // 使用split分割字符串
                    DataPart = readLine.split(":");
                    DataPart = DataPart[1].split("\s+");
    
                    if (Transmit) {
                        // 获取接收的总流量
                        Data += Integer.parseInt(DataPart[1]);
                    } else {
                        // 获取上传的总流量
                        Data += Integer.parseInt(DataPart[9]);
                    }
                }
                // 关闭文件
                fr.close();
                br.close();
            } catch (IOException e) {
                // 获取失败则返回-1
                return -1;
            }
            // 返回数据的总字节
            return Data;
        }
    
        public Traffic(Context context) {
            this(context, null);
        }
    
        public Traffic(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public Traffic(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        // 初始化工作
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            // 延迟Call线程
            mHandler.postDelayed(task, mDelaytime);
        }
    
        // 销毁工作
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            // 移除线程
            mHandler.removeCallbacks(task);
        }
    
        private static final String BYTES = "B/s";
        private static final String MEGABYTES = "MB/s";
        private static final String KILOBYTES = "KB/s";
        private static final String GIGABYTES = "GB/s";
        private static final long KILO = 1024;
        private static final long MEGA = KILO * 1024;
        private static final long GIGA = MEGA * 1024;
    
        // 格式化数据
        static String formatSize(final long pBytes) {
            if (pBytes < KILO) {
                return pBytes + BYTES;
            } else if (pBytes < MEGA) {
                return (int) (0.5 + (pBytes / (double) KILO)) + KILOBYTES;
            } else if (pBytes < GIGA) {
                return (int) (0.5 + (pBytes / (double) MEGA)) + MEGABYTES;
            } else {
                return (int) (0.5 + (pBytes / (double) GIGA)) + GIGABYTES;
            }
        }
    }
  • 相关阅读:
    网站设计大访问量应用的解决方案
    粘贴剪辑版中的数据
    C#中DateTime
    汇总c#.net常用函数和方法集
    ASP.NET配置文件Web.config 详细解释
    URL验证
    把一个下拉框中的选项添加到另一个中
    显示年月日星期和(变动的)时间
    sqlserver 查询版本号
    FreeMarker(三)Map和List
  • 原文地址:https://www.cnblogs.com/GentlemanMod/p/3276566.html
Copyright © 2020-2023  润新知