5

I am trying to fetch mobile data usage using TrafficStats.

Example:

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

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

In few devices it is working absolutely fine. In Nexus 5X I am getting totalUsage but mobileDataUsage is always 0, TrafficStats.getMobileRxBytes() and TrafficStats.getMobileTxBytes() always return 0.

1 Answers1

2

You most probably are seeing the issue described at https://code.google.com/p/android/issues/detail?id=78924 .

The docs for TrafficStats says the bytes returned "always increases monotonically since device boot", but that is indeed not the case for at least Android 5-7. Often the methods return 0 and then suddenly start counting from the number they used to have.

Roy Solberg
  • 18,133
  • 12
  • 49
  • 76