Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 543
Aakash Jain Avatar asked Oct 31 '22 02:10

Aakash Jain


1 Answers

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.

like image 99
Roy Solberg Avatar answered Nov 15 '22 05:11

Roy Solberg