安卓流量监控工具类------TrafficStats

1. android实现流量监控在2.2版本之前没有专门的类和接口,我网上查看的大家的方法基本上都是读取一个文件/proc/net/dev,这个方法我没有使用,没有进行深入研究。

2. 我是用的是android2.2及之后版本有的流量监控类,TrafficStats,这个类里提供了以下几种方法,

static long getMobileRxBytes()//获取通过Mobile连接收到的字节总数,但不包含WiFi
static long getMobileRxPackets()//获取Mobile连接收到的数据包总数
static long getMobileTxBytes()//Mobile发送的总字节数
static long getMobileTxPackets()//Mobile发送的总数据包数
static long getTotalRxBytes()//获取总的接受字节数,包含Mobile和WiFi等
static long getTotalRxPackets()//总的接受数据包数,包含Mobile和WiFi等
static long getTotalTxBytes()//总的发送字节数,包含Mobile和WiFi等
static long getTotalTxPackets()//发送的总数据包数,包含Mobile和WiFi等
static long getUidRxBytes(int uid)//获取某个网络UID的接受字节数
static long getUidTxBytes(int uid) //获取某个网络UID的发送字节数

原文地址:https://www.cnblogs.com/android-er/p/5335325.html