Android TrafficStats.getTotalRxBytes() 低于预期

发布于 2024-12-20 17:53:37 字数 1473 浏览 1 评论 0原文

我正在尝试获取真实的流量统计数据。但是,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 求和流量统计数据吗?

I'm trying to get а real traffic stats data. But, TrafficStats.getTotalRxBytes() is less than a sum of TrafficStats.getUidRxBytes() for each installed application.
I've found it out by running each 30 seconds this code (being on a Wi-Fi network):

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

And this is in logs:

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

The difference between totals is less than difference between sums :

9320030 - 9314216 = 5814 bytes // sums of traffic stats by uid
4246524 - 4245915 = 609 bytes // total traffic stats

1) Why is that?
2) How do i get a correct value for total traffic stats - should i just sum traffic stats by uids?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文