如何计算Android中每个应用程序的移动和wifi数据使用情况? [英] How to calculate mobile and wifi data usage of each application in android?

查看:93
本文介绍了如何计算Android中每个应用程序的移动和wifi数据使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用TrafficStats':(getUidRxBytes,getUidTxBytes,getTotalRxbytes,getTotalTXbytes,getMobileRxBytes,getMobileTxBytes)方法是否可以计算出Android中每个应用程序的移动和wifi使用率?我知道一定有某种方法可以做到,因为3G看门狗和其他一些应用程序提供了这些详细信息.

Is there any possible way for calculating mobile and wifi usage of each application in android using TrafficStats' : (getUidRxBytes,getUidTxBytes, getTotalRxbytes, getTotalTXbytes, getMobileRxBytes,getMobileTxBytes) methods ? I know there must be some way of doing that as 3G watchdog and some other application provide these details.

有人可以帮忙吗?谢谢

推荐答案

我对此类问题

I've written a similar answer to this type of question here. For getting overall data usage per app it's fairly easy using the methods you named, TrafficStats.getUidTxBytes(int) and TrafficStats.getUidRxBytes(int). However, breaking into mobile and Wifi is not something publicly available currently.

正如我在其他答案中提到的那样,您必须深入研究内部框架API,并尝试复制那里使用的一些方法.您肯定需要使用 NetworkTemplate.buildTemplateWifiWildcard()

As I mention in my other answer, you'll have to dig into the internal frameworks APIs and try to copy some of the methods used there. You'll definitely need to use NetworkTemplate.buildTemplateWifiWildcard() and NetworkTemplate.buildTemplateMobileWildcard() in order to retrieve the correct NetworkStats for each interface. Once you have the NetworkStats object you'll be able to get your info. Just note: if you're using this in a public app, using internal APIs is very likely to break whenever Google decides to update them.

作为另一个提示,您应该查看设置"应用如何能够显示每个应用的流量并通过移动/Wifi进行分隔的来源.特别是,您可以在这里查看其

as another tip, you should check out the source for how the Settings app is able to display traffic per app and separate it by Mobile/Wifi. In particular you can look here at their ChartDataLoader, particularly the methods collectHistoryForUid() and loadInBackground()

这篇关于如何计算Android中每个应用程序的移动和wifi数据使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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