Android:如何获取每日或按时间范围的Internet数据使用情况?

时间:2022-10-20 16:30:23

I am trying to get the exact Data Usage per Day , or by Range of Time in Android

我试图获取每天的确切数据使用情况,或者按照Android中的时间范围获取

however from what I searched, I cannot found anything that could do this

然而,根据我搜索的内容,我找不到任何能够做到这一点的事情

Something that I tried

我试过的东西

TrafficStats

this one will reset all data every time the device is boot, so I couldn't use it

这个将在每次设备启动时重置所有数据,因此我无法使用它


NetworkPolicyManager

this one require system permission. So, I can't use it

这个需要系统许可。所以,我不能用它

any help would be appreciate

任何帮助将是欣赏

1 个解决方案

#1


0  

You need to have a service that will periodically call TrafficStats APIs to get current statistics, and store the results. E.g. the delta between two invocations of getMobileTxBytes() is the number of bytes received during that time period.

您需要拥有一个定期调用TrafficStats API以获取当前统计信息并存储结果的服务。例如。 getMobileTxBytes()的两次调用之间的增量是在该时间段内接收的字节数。

Of course, if someone just pulls a battery out of their phone, you'll lose statistics since the last invocation of your service. So set the frequency of updates according to your needs (every hour versus every minute).

当然,如果有人只是从手机中取出电池,那么自上次调用服务以来,您将丢失统计信息。因此,根据您的需求设置更新频率(每小时与每分钟)。

#1


0  

You need to have a service that will periodically call TrafficStats APIs to get current statistics, and store the results. E.g. the delta between two invocations of getMobileTxBytes() is the number of bytes received during that time period.

您需要拥有一个定期调用TrafficStats API以获取当前统计信息并存储结果的服务。例如。 getMobileTxBytes()的两次调用之间的增量是在该时间段内接收的字节数。

Of course, if someone just pulls a battery out of their phone, you'll lose statistics since the last invocation of your service. So set the frequency of updates according to your needs (every hour versus every minute).

当然,如果有人只是从手机中取出电池,那么自上次调用服务以来,您将丢失统计信息。因此,根据您的需求设置更新频率(每小时与每分钟)。