如何查看應用的流量消耗?


1.首先需求獲取應用的UID

adb shell dumpsys package com.android.mms |findstr userId=

2.然后...

adb shell cat /proc/net/xt_qtaguid/stats | findstr userId

 

48 wlan0 0x0 10127 0 316574 2279 472562 3651 316574 2279 0 0 0 0 472562 3651 0 0 0 0
49 wlan0 0x0 10127 1 6172960 4936 415951 5215 6172960 4936 0 0 0 0 415951 5215 0 0 0 0
50 wlan0 0x3792d5b400000000 10127 0 29678 208 32168 296 29678 208 0 0 0 0 32168 296 0 0 0 0
51 wlan0 0x3792d5b400000000 10127 1 226170 222 25745 265 226170 222 0 0 0 0 25745 265 0 0 0 0
56 wlan0 0xfa1dcc4b00000000 10127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
57 wlan0 0xfa1dcc4b00000000 10127 1 3014885 2127 139857 2117 3014885 2127 0 0 0 0 139857 2117 0 0 0 0

其中第6和8列為 rx_bytes(接收數據)和tx_bytes(傳輸數據)包含tcp,udp等所有網絡流量傳輸的統計。

整成了一個腳本:

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class getflow {
    public static void main(String[] args) throws Exception
    {
        ArrayList<String> packages=getpackage();    
        for(int i=0;i<packages.size();i++)
        {  
            String uid=getuid(packages.get(i));
            System.out.println("包名為"+packages.get(i)+"的WIFI流量消耗為:"+getliuliangwifi(uid)/1024+"KB");
            System.out.println("包名為"+packages.get(i)+"的GPRS流量消耗為:"+getliulianggprs(uid)/1024+"KB");
            System.out.println("......");
        }
        System.out.println("所有的應用流量消耗已獲取完成");
    }
    public static ArrayList<String> getpackage() throws Exception
    {   
        
        Process p=Runtime.getRuntime().exec("adb shell pm list packages -3");
        InputStream in=p.getInputStream();
        InputStreamReader ir=new InputStreamReader(in);
        BufferedReader br=new BufferedReader(ir);
        String str;
        ArrayList<String> list=new ArrayList<>();
        while((str=br.readLine())!=null)
        {   
            list.add(str.trim().split(":")[1]);
            str=br.readLine();
            
        }
        return list;
    }
    public static String getuid(String packagename) throws Exception
    {   
        Process p=Runtime.getRuntime().exec("adb shell dumpsys package "+packagename+" |grep userId");
        InputStream in=p.getInputStream();
        InputStreamReader ir=new InputStreamReader(in);
        BufferedReader br=new BufferedReader(ir);
        String uid=br.readLine().split("=")[1].split(" ")[0];
        return uid;
    }
    public static float getliuliangwifi(String uid) throws IOException
    {   
        Process p=Runtime.getRuntime().exec("adb shell cat /proc/net/xt_qtaguid/stats |grep "+uid+" |grep wlan0");
        InputStream in=p.getInputStream();
        InputStreamReader ir=new InputStreamReader(in);
        BufferedReader br=new BufferedReader(ir);
        String str;
        float total=0;
        while((str=br.readLine())!=null)
        {   
            total=total+Integer.parseInt(str.split(" ")[5])+Integer.parseInt(str.split(" ")[7]);
            str=br.readLine();
        }
        return total;
    }
    
    public static float getliulianggprs(String uid) throws IOException
    {   
        Process p=Runtime.getRuntime().exec("adb shell cat /proc/net/xt_qtaguid/stats | grep "+uid+" |grep rmnet0");
        InputStream in=p.getInputStream();
        InputStreamReader ir=new InputStreamReader(in);
        BufferedReader br=new BufferedReader(ir);
        String str;
        float total=0;
        while((str=br.readLine())!=null)
        {
            total=total+Integer.parseInt(str.split(" ")[5])+Integer.parseInt(str.split(" ")[7]);
            str=br.readLine();
        }
        return total;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM