TrafficStats Api android和每日数据使用量的计算 [英] TrafficStats Api android and calculation of daily data usage

查看:25
本文介绍了TrafficStats Api android和每日数据使用量的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Android的TrafficStats的以下两种方法有混淆:getUidTxBytes(int uid) 和 getUidRxBytes(int uid) ,这两个方法返回为此 UID 通过网络传输和接收的字节数.但是它的时间单位是什么,是每秒吗?如果我想计算每个应用程序每天传输和接收的数据,我该怎么做.我想到了一种方法,将数据存储在 sql 中并继续向表中添加数据.方法对吗?

Have a confusion over following two methods of TrafficStats of Android: getUidTxBytes(int uid) and getUidRxBytes(int uid) , These two methods return the number of bytes transmitted and received through the network for this UID. But what is the time unit of it, is it per second? If I want to calculate data transmitted and received per day per app, what should I do. I thought one way, to store data in sql and keep on adding data to the table. Is it proper way?

推荐答案

这些是因为接口上升"或因为具有此 UID 的应用程序已经启动"的计数器.因此,假设您的手机进入飞行模式"然后返回,计数器可能会再次从零开始.如果需要每秒值,则需要每秒调用这些函数,然后使用上次调用的增量.如果 delta 是负数,就按原样使用这个值,这意味着计数器再次从零开始.

These are counters "since the interface went up" or "since the application with this UID has started". So say if your phone goes into "Airplane mode" and then back, the counters might start from zero again. If you need per-second values, you'll need to call these functions every second, and then use the delta from the last call. If the delta is negative, just use the value as-is, it means the counter started from zero again.

还有一件事:据我所知,这些计数器只计算 TCP/IP.不是 UDP.因此,如果您需要非常精确的记帐,并且所讨论的应用程序使用 UDP/IP 或除 TCP 之外的任何其他协议,则这些计数器将是错误的.

One more thing: As far as I know, these counters count TCP/IP only. Not UDP. So if you need a very precise accounting, and the application in question uses UDP/IP, or any other protocol besides TCP, these counters will be wrong.

要深入了解此功能的工作原理,请查看免费提供的 Android 源代码.有问题的文件是 ./frameworks/base/core/jni/android_net_TrafficStats.cpp

For the insight how this function works, look at the source of Android, freely available. File in question is ./frameworks/base/core/jni/android_net_TrafficStats.cpp

该函数从/proc/uid_stat/[uid]/tcp_snd获取数据.如果您需要更多相关信息,则需要深入了解 Linux 内核...

This function gets the data from /proc/uid_stat/[uid]/tcp_snd. If you need more info about that, you'll need to dive into the Linux kernel...

这篇关于TrafficStats Api android和每日数据使用量的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆