軟件流量使用數據保存在 /proc/uid_stat/uid(用戶id)/ 下面文件中
/proc/uid_stat/uid/tcp_send 上傳流量
/proc/uid_stat/uid/tcp_rcv 下載流量
關鍵代碼如下:
//1.獲取一個包管理器。
PackageManager pm = getPackageManager();
//2.遍歷手機操作系統 獲取所有的應用程序的uid
List<ApplicationInfo> appliactaionInfos = pm.getInstalledApplications(0);
for(ApplicationInfo applicationInfo : appliactaionInfos){
int uid = applicationInfo.uid; // 獲得軟件uid
//proc/uid_stat/10086
long tx = TrafficStats.getUidTxBytes(uid);//發送的 上傳的流量byte
long rx = TrafficStats.getUidRxBytes(uid);//下載的流量 byte
//方法返回值 -1 代表的是應用程序沒有產生流量 或者操作系統不支持流量統計
}
TrafficStats.getMobileTxBytes();//獲取手機3g/2g網絡上傳的總流量
TrafficStats.getMobileRxBytes();//手機2g/3g下載的總流量
TrafficStats.getTotalTxBytes();//手機全部網絡接口 包括wifi,3g、2g上傳的總流量
TrafficStats.getTotalRxBytes();//手機全部網絡接口 包括wifi,3g、2g下載的總流量