TrafficStats阿比Android和日常数据使用量的计算使用量、日常、阿比、数据

2023-09-12 09:53:28 作者:情场浪子

有一个混乱以下的Andr​​oid TrafficStats两种方法: getUidTxBytes(INT UID)和getUidRxBytes(INT UID) 这两种方法返回的字节数发送的,并通过接收到的 在网络这个UID.But什么是它的时间单位,它是每秒。 如果我想计算传输和每一天的应用程序接收到的数据,又该 我愿意。我thoght的一种方式,将数据存储在SQL,不断将数据添加到table.Is它正确的方式。

Have a confusion over following two methods of TrafficStats of Android: getUidTxBytes(int uid) and getUidRxBytes(int uid) , These two methods returns 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 thoght one way, to store data in sql and keep on adding data to the table.Is it proper way.

推荐答案

这些都是专柜,因为该接口上升或自从有了这个UID的应用程序已启动。所以说,如果你的手机进入飞行模式,然后回来,计数器会从零重新开始。如果您需要每秒值,你需要调用这些函数每一秒,然后用增量的最后一次通话。如果增量为负,只使用值-是,这意味着计数器再次从零开始。

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。所以,如果你需要一个非常precise核算,有问题的应用程序使用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.

有关如何此功能的工作洞察力,看的Andr​​oid源代码,免费提供。文件的问题是 ./框架/基/核心/ 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...