我正在尝试获取真实的流量统计数据。但是,TrafficStats.getTotalRxBytes() 小于每个已安装应用程序的 TrafficStats.getUidRxBytes() 之和。
我通过每 30 秒运行一次这段代码(在 Wi-Fi 网络上)发现了这一点:
long total = TrafficStats.getTotalRxBytes();
long mobileTotal = TrafficStats.getMobileRxBytes();
long wifiTotal = (total - mobileTotal);
Log.v("SSS", "total=" + total + " mob=" + mobileTotal + " wifi=" + wifiTotal);
ArrayList<Integer> uids = new ArrayList<Integer>();
int cnt = 0;
for (ApplicationInfo appInfo : packageManager.getInstalledApplications(0)) {
long bytes = TrafficStats.getUidRxBytes(appInfo.uid);
if (!uids.contains(appInfo.uid)) {
// few applications can refer to same uid - sum each uid stats only once
uids.add(appInfo.uid);
if (bytes > 0) cnt += bytes;
}
}
Log.v("SSS", "sum traffic stats = " + cnt);
这是在日志中:
12-12 19:43:18.798: V/SSS(4772): total=4245863 mob=0 wifi=4245863
12-12 19:43:19.569: V/SSS(4772): sum traffic stats = 9313461
12-12 19:43:49.628: V/SSS(4772): total=4245915 mob=0 wifi=4245915
12-12 19:43:50.459: V/SSS(4772): sum traffic stats = 9314216
12-12 19:44:20.518: V/SSS(4772): total=4246524 mob=0 wifi=4246524
12-12 19:44:21.439: V/SSS(4772): sum traffic stats = 9320030
总计之间的差异小于总和之间的差异:
9320030 - 9314216 = 5814 bytes // sums of traffic stats by uid
4246524 - 4245915 = 609 bytes // total traffic stats
1)这是为什么?
2) 我如何获得总流量统计数据的正确值 - 我应该只按 uid 求和流量统计数据吗?
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)