/**
?*?獲得本次開機(jī)后使用的流量
?*/
????public?void?getData()?{
????????//PackageManager?包管理類
????????PackageManager?packageManager?=?context.getPackageManager();
????????int?PackageUid?=?0;
????????BigDecimal?numRx?=?new?BigDecimal("0");
????????BigDecimal?numTx?=?new?BigDecimal("0");
????????
????????/**
?????????*?循環(huán)抓緊所有應(yīng)用的包名
?????????*?和當(dāng)前應(yīng)用的包名進(jìn)行匹配操作
?????????*?把獲取到的UID保存到一個(gè)臨時(shí)變量
?????????*/
????????for(ApplicationInfo?info?:?packageManager.getInstalledApplications(0)){
????????????int?uid?=?info.uid;
????????????String?packageName?=?info.packageName;
????????????if(packageName.equals(PACKAGENAME)){
????????????????PackageUid?=?uid;
????????????}
????????}
????????//TrafficStats類根據(jù)應(yīng)用的UID獲取到流量的相關(guān)數(shù)據(jù)
????????long?rx?=?TrafficStats.getUidRxBytes(PackageUid);//總接收量
????????long?tx?=?TrafficStats.getUidTxBytes(PackageUid);//總發(fā)送量
????????
????????LogManager.show(TAG,?"本應(yīng)用本次開機(jī)總接收量:"+rx,?1);
????????LogManager.show(TAG,?"本應(yīng)用本次開機(jī)總發(fā)送量:"+tx,?1);
????????if(rx>0){
???????? numRx?=?byteToM(rx);
????????}
????????if(tx>0){
???????? numTx?=?byteToM(tx);
????????}
????????
????????LogManager.show(TAG,?"轉(zhuǎn)換為(M為單位)后的總接收量:"+numRx.floatValue(),?1);
????????LogManager.show(TAG,?"轉(zhuǎn)換為(M為單位)后的總發(fā)送量:"+numTx.floatValue(),?1);
????????
????}
????/**
?????*?字節(jié)轉(zhuǎn)換為M
?????*/
????private?BigDecimal?byteToM(long?value){
???? BigDecimal?result?=?new?BigDecimal(Long.toString(value));
???? result?=?result.divide(new?BigDecimal("1024.0")).divide(new?BigDecimal("1024.0"));
???? return?result;
????}