TrafficStats.getMobileRxBytes () and TrafficStats.getMobileTxBytes () always return 0 in Nexus 5x

I am trying to use mobile data usage with TrafficStats.

Example:

mobileDataUsage = TrafficStats.getMobileRxBytes () + TrafficStats.getMobileTxBytes ()

totalUsage = TrafficStats.getTotalRxBytes () + TrafficStats.getTotalTxBytes ()

In several devices, it works absolutely fine. In Nexus 5X, I get totalUsage, but mobileDataUsage is always 0, TrafficStats.getMobileRxBytes() and TrafficStats.getMobileTxBytes() always returns 0.

+5
source share
1 answer

You most likely see the problem described at https://code.google.com/p/android/issues/detail?id=78924 .

docs for TrafficStats says that the returned bytes "always increase monotonously from the moment the device boots up," but this is really not the case, at least for Android 5-7. Often, methods return 0, and then suddenly start counting from the number that they had.

+2
source

All Articles