How to get mobile data usage for my application (e.g. using TrafficStats)

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

The corresponding TrafficStats API, which receives its data, for example. / Proc / uid _stat /.

Statistics should include GCM, etc., and not just the material that I run, so the counters with the stream and tag are not sufficient.

The first problem is that there is clearly no API for using UID-based mobile data (i.e. for each application). There is a getMobile API and getUid API, but nothing that unites them. I propose a solution for this as one answer below.

More importantly, I get the impression that TrafficStats has too many errors that can be used for this general purpose. I also added this as a possible answer, and if that is accepted, the best solution would be to manually calculate the data usage.

+6
source share
2 answers

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 .

+2
source

Here is my initial decision. The API is actually broken down in several ways, both old and new, so this is just a solution that you can use if fixed.

Getting statistics for your application is simple:

int uid = android.os.Process.myUid(); txBytesInitial = TrafficStats.getUidTxBytes(uid); rxBytesInitial = TrafficStats.getUidRxBytes(uid); 

Thus, every time a network connection is lost (or the application is closed, or statistics are required by the application logic), the delta in the use of your application data is added to your statistics based on NetworkInfo.getType (). You would need to save the last one that was extracted for use in calculating the delta, and they should be stored in, for example, SharedPreferences and reset when the phone restarts or when they go in the wrong direction (depending on the device, they may be reset for other reasons, but then reboot).

See this useful SO link for some additional explanation about these APIs (for example, they do not include package overhead, just the payload).

+3
source

Source: https://habr.com/ru/post/956196/


All Articles