相关疑难解决方法(0)

如何获取TrafficStats中发送和接收的正确字节数?

我的应用程序试图计算通过WiFi/LAN和移动数据连接发送和接收的字节数.为此,我TrafficStats在一个时间点获取计数器的值,并在下次检查时从其值中减去该值.

// get current values of counters
long currentMobileTxBytes = TrafficStats.getMobileTxBytes();
long currentMobileRxBytes = TrafficStats.getMobileRxBytes();
long totalTxBytes = TrafficStats.getTotalTxBytes();
long totalRxBytes = TrafficStats.getTotalRxBytes();

// to get mobile data count, subtract old from current
long currentMobileSent = currentMobileTxBytes - oldMobileTxBytes;
long currentMobileReceived = currentMobileRxBytes - oldMobileRxBytes;

// to get WiFi/LAN data count, subtract total from mobile
long currentNetworkSent = totalTxBytes - currentMobileTxBytes;
long currentNetworkReceived = totalRxBytes - currentMobileRxBytes;
Run Code Online (Sandbox Code Playgroud)

我觉得上面的算法是合理的,但是,我不知道如何检查这些计数器的准确性.例如,当我尝试通过WiFi将2.7MB文件上传到Dropbox时,currentMobileSent我得到的值大约是10MB.即使没有浏览网页直到下一次检查,我得到非零值,表明我在等待期间确实收到了一些字节的数据.

有没有办法让我检查TrafficStats这些数字是如何到达的?我知道除了我的浏览器,可能还有其他应用程序在后台运行连接到互联网,但2.7MB到10MB似乎是一个巨大的跳跃 - 我甚至"没有做任何事情"一次"收到"90MB.或者我计算发送和接收的字节的方式有问题吗?

android traffic network-traffic traffic-measurement

11
推荐指数
1
解决办法
2万
查看次数

TrafficStats Api android和日常数据使用量的计算

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

networking android usage-statistics android-data-usage

10
推荐指数
2
解决办法
1万
查看次数