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;
        }
    }
}
原文地址:https://www.cnblogs.com/GentlemanMod/p/3276566.html