Reputation: 51
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.
Upvotes: 5
Views: 1778
Reputation: 19703
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.
Upvotes: 2