如何让移动数据使用我的应用程序(例如,使用TrafficStats) [英] How to get the mobile data usage for my app (e.g. using TrafficStats)

查看:318
本文介绍了如何让移动数据使用我的应用程序(例如,使用TrafficStats)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我认为这是一个普遍的问题,但请不要太快,因为我相信我的答案,添加新的东西来讨论标记为重复的。)

(I think this is a common question but please don't mark as duplicate too quickly as I believe my answers add something new to the discussion.)

相关API是 TrafficStats ,从如中获取数据的/ proc / uid_stat /.

The relevant API is TrafficStats, which gets its data from e.g. /proc/uid_stat/.

的统计数据必须包括GCM等,不只是东西,我开始,所以线程和基于标签的专柜是不够的。

The stats must include GCM etc., not just stuff I initiate, so the thread and tag based counters are not sufficient.

第一个问题是一个API来获取基于UID(即每个应用程序)的移动数据使用量是显眼。有getMobile API和的getuid的API,但没有结合他们。我提出了一个解决方案,这是一个答案如下:

The first problem is an API to get mobile data usage based on UID (i.e. per app) is conspicuously absent. There are getMobile API's and getUid API's, but nothing that combines them. I propose a solution to this as one answer below.

更重要的是,我发现了IM pression的TrafficStats有太多的虫子是这个一般用途使用。我把那作为一个可能的答案也一样,如果被接受的话,最好的解决办法是手动计数数据使用。

More importantly, I'm getting the impression that TrafficStats has too many bugs to be usable for this general purpose. I put that as a possible answer too, and if that is accepted then the best solution is to count data usage manually.

推荐答案

我认为最好的解决办法是使用类似Wireshark的测量所使用的应用程序的各种网络运行数据,然后手动跟踪数据消费。这肯定是做-能。

I think the best solution is to use something like wireshark to measure the data used by the various network operations of your app, and then track your data consumption manually. Which is certainly do-able.

我说的话,因为TrafficStats的错误是积累的速度,然后他们被固定的,和补丁被忽略了,所以我有TrafficStats没有得到维护,应避免IM pression。 JBQ分配很多之前,他退出了,但我相信他只是清理。我意识到,Android设置包括数据使用状态。我没有调查,但猜想,这些也是不可靠的(不好意思,我是懒惰,但我已经得出结论,我会浪费时间要少得多,如果我只是手动做我的计算)。

I say that because the bugs in TrafficStats are accumulating faster then they are being fixed, and patches are being ignored, so I have the impression that TrafficStats isn't being maintained and should be avoided. JBQ assigned many before he quit, but I believe he was just cleaning up. I realize that Android settings includes data usage stats. I have not investigated but would guess that those are also not reliable (sorry that I'm being lazy but I've already concluded that I would have wasted much less time if I just did my calculations manually).

下面是一些错误的trafficstats,我碰到(有些问题在TraffficStats,一些起源于内核中),而在看普通的API。我没有尝试过的线程/标签/基于socket API的,并且不打算根据我的经验,到目前为止,但有兴趣听,如果他们更好地工作。

Here are some of the bugs in trafficstats that I came across (some of the issues are in TraffficStats, some originate in the kernel) while looking at general API's. I have not tried the thread/tag/socket based API's, and don't plan to based on my experience so far, but would be interested to hear if they work better.

在2.X
<一href="http://stackoverflow.com/questions/8478696/android-trafficstats-gettotalrxbytes-is-less-than-expected">Android TrafficStats.getTotalRxBytes()小

In 2.x
Android TrafficStats.getTotalRxBytes() is less than expected

在3.X
TrafficStats.getMobileRxBytes()和TrafficStats.getMobileTxBytes():
的https://$c$c.google.com/p /安卓/问题/详细信息?ID = 19938
TrafficStats的API不报告,即使空气污染指数12日起,他们应该得到支持(从来没有固定)UDP流量:
的https://$c$c.google.com/p /安卓/问题/详细信息?ID = 32410

In 3.x
TrafficStats.getMobileRxBytes() and TrafficStats.getMobileTxBytes():
https://code.google.com/p/android/issues/detail?id=19938
TrafficStats APIs do not report UDP traffic even though API level 12 onwards they should have been supported (never fixed):
https://code.google.com/p/android/issues/detail?id=32410

在4.3中,的getuid的API完全被打破,但它听起来就像他们可能计划来解决这个问题。无论如何,如果bug上场谷歌的合作伙伴,然后我觉得这些API的是无用的,在可预见的未来。

In 4.3, the getUid API's are totally broken, but it sounds like they might be planning to fix this bug. Regardless, if the bug has gone out to Google's partners then I think these API's are useless for the forseeable future.

TrafficStats.getUidRxBytes和getUidTxBytes始终返回0,在4.3:
的https://$c$c.google.com/p /安卓/问题/详细信息?ID = 58210
而看到<一href="http://stackoverflow.com/questions/17894966/getuidrxbytes-and-getuidtxbytes-always-return-0-in-android-4-3">this SO帖子。

TrafficStats.getUidRxBytes and getUidTxBytes always return 0 in 4.3:
https://code.google.com/p/android/issues/detail?id=58210
And see this SO post.

这篇关于如何让移动数据使用我的应用程序(例如,使用TrafficStats)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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