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; } }