1、获得应用的Pid。
可以通过adb shell ps获得所有进程信息,里面当然包含应用的pid和包名。然后对返回信息进行查找(按包名查找)。
C:\Users>adb shell ps
USER PID PPID VSIZE RSS WCHAN PC NAME
root 1 0 17336 1668 SyS_epoll_ 0000000000 S /init
root 2 0 0 0 kthreadd 0000000000 S kthreadd
root 3 2 0 0 smpboot_th 0000000000 S ksoftirqd/0
root 5 2 0 0 worker_thr 0000000000 S kworker/0:0H
root 7 2 0 0 rcu_gp_kth 0000000000 S rcu_preempt
root 8 2 0 0 rcu_gp_kth 0000000000 S rcu_sched
root 9 2 0 0 rcu_gp_kth 0000000000 S rcu_bh
root 10 2 0 0 nocb_leade 0000000000 S rcuop/0
root 11 2 0 0 nocb_leade 0000000000 S rcuos/0
root 12 2 0 0 nocb_leade 0000000000 S rcuob/0
root 13 2 0 0 smpboot_th 0000000000 S rcuc/0
root 14 2 0 0 rcu_boost_ 0000000000 S rcub/0
root 15 2 0 0 smpboot_th 0000000000 S migration/0
root 16 2 0 0 smpboot_th 0000000000 S migration/1
root 17 2 0 0 smpboot_th 0000000000 S rcuc/1
root 18 2 0 0 smpboot_th 0000000000 S ksoftirqd/1
root 20 2 0 0 worker_thr 0000000000 S kworker/1:0H
root 21 2 0 0 rcu_nocb_k 0000000000 S rcuop/1
root 22 2 0 0 rcu_nocb_k 0000000000 S rcuos/1
上面的截图还有没有截取全;找到你自己报名的PID,后面使用
2、获得应用的Uid.
Android系统的目录文件/proc/pid/status(注意:标红的pid是第1步中获得的pid,是一个大于0的数字)中包含关于uid的信息。以pid=15904为例,通过adb shell cat /proc/15904/status可以得到以下信息:
C:\Users>adb shell
msm8953_64:/ $ cat /proc/15904/status
cat /proc/15904/status
Name: com.******
State: S (sleeping)
Tgid: 15904
Pid: 15904
PPid: 755
TracerPid: 0
Uid: 10092 10092 10092 10092
Gid: 10092 10092 10092 10092
Ngid: 0
FDSize: 256
Groups: 3002 3003 9997 50092
VmPeak: 1666848 kB
VmSize: 1218200 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 129268 kB
VmRSS: 125288 kB
VmData: 244900 kB
VmStk: 8196 kB
VmExe: 20 kB
VmLib: 144944 kB
VmPTE: 972 kB
VmSwap: 380 kB
Threads: 64
SigQ: 0/10488
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000001204
SigIgn: 0000000000000000
SigCgt: 20000002000084f8
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000000000000000
Seccomp: 0
Cpus_allowed: ff
Cpus_allowed_list: 0-7
voluntary_ctxt_switches: 2751
nonvoluntary_ctxt_switches: 417
解析以上信息即可得到Uid.
3、读取流量值。
Android系统的目录文件/proc/uid_stat/uid/下一般会有两个文件tcp_snd和tcp_rcv。(注意:标红的uid是第二步中获得的Uid数值)。通过adb shell cat /proc/uid_stat/10092/tcp_snd和adb shell cat /proc/uid_stat/10092/tcp_rcv即可分别获得上行流量和下行流量。
127|msm8953_64:/ $ ls /proc/uid_stat/10092
ls /proc/uid_stat/10092
tcp_rcv tcp_snd
msm8953_64:/ $ cat /proc/uid_stat/10092/tcp_snd
cat /proc/uid_stat/10092/tcp_snd
119973585
msm8953_64:/ $ cat /proc/uid_stat/10092/tcp_snd
cat /proc/uid_stat/10092/tcp_rcv
123436113
后面提供一下adb的部分命令:
adb devices 列出所有设备
adb -s 设备名称 shell 进入对应的设备
cd proc 进入设备的属性目录
cd uid_stat 进入 user id 状态目录,每个应用程序在安装的时候系统会为每个应用分配一个对应的 uid
ls 列出 uid_stat 目录下所有应用对应的 user id 目录
cd uid 进入对应应用的 uid 目录
ls 查看对应 uid 目录下的 tcp_rcv 和 tcp_snd 目录
cat tcp_rcv 查看该应用接收的数据信息
cat tcp_snd 查看该应用发送的数据信息
后面再转载一下其他大神的解析tcp_send代码:
private Long getTotalBytesManual(int localUid) {
// Log.e("BytesManual*****", "localUid:" + localUid);
File dir = new File("/proc/uid_stat/");
String[] children = dir.list();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < children.length; i++) {
stringBuffer.append(children[i]);
stringBuffer.append(" ");
}
// Log.e("children*****", children.length + "");
// Log.e("children22*****", stringBuffer.toString());
if (!Arrays.asList(children).contains(String.valueOf(localUid))) {
return 0L;
}
File uidFileDir = new File("/proc/uid_stat/" + String.valueOf(localUid));
File uidActualFileReceived = new File(uidFileDir, "tcp_rcv");
File uidActualFileSent = new File(uidFileDir, "tcp_snd");
String textReceived = "0";
String textSent = "0";
try {
BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived));
BufferedReader brSent = new BufferedReader(new FileReader(uidActualFileSent));
String receivedLine;
String sentLine;
if ((receivedLine = brReceived.readLine()) != null) {
textReceived = receivedLine;
// Log.e("receivedLine*****", "receivedLine:" + receivedLine);
}
if ((sentLine = brSent.readLine()) != null) {
textSent = sentLine;
// Log.e("sentLine*****", "sentLine:" + sentLine);
}
} catch (IOException e) {
e.printStackTrace();
// Log.e("IOException*****", e.toString());
}
// Log.e("BytesManualEnd*****", "localUid:" + localUid);
return Long.valueOf(textReceived).longValue() + Long.valueOf(textSent).longValue();
}
各位大神做的不对的地方请指出,谢谢