I think the best solution is to use something like wirehark to measure the data used by the various network operations of your application, and then track the data consumption manually. It is certainly capable.
I say that, since errors in TrafficStats accumulate faster, they are fixed and corrections are ignored, so I get the impression that TrafficStats is not supported and should be avoided. JBQ appointed a lot before he left, but I think he just cleaned up. I understand that Android settings include data usage statistics. I did not research, but I would assume that they are also unreliable (sorry that I am lazy, but I have already come to the conclusion that I would have spent less time if I had only done my calculations manually).
Here are some of the errors in trafficstats that I came across (some of them relate to TraffficStats, some of them relate to the kernel), looking at common APIs. I have not tried the thread / tag / socket API and do not plan to build on my experience so far, but it would be interesting to hear if they work better.
In 2.x
Android TrafficStats.getTotalRxBytes () is less than expected
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 should be supported (never fixed):
https://code.google.com/p/android/issues/detail?id=32410
In 4.3, the getUid API is completely broken, but it looks like they can fix this error. Regardless if the error occurred with Google partners, I think these APIs are useless for the foreseeable future.
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 .
source share