How to get the correct number of bytes sent and received in TrafficStats?
My app is trying to count the number of bytes send and received over WiFi/LAN and mobile data connections. To do that, I get the values of TrafficStats counters at one point in time and subtract that from its values the next time I check. // get current values of counters long currentMobileTxBytes = TrafficStats.getMobileTxBytes(); long currentMobileRxBytes = TrafficStats.getMobileRxBytes(); long totalTxBytes = TrafficStats.getTotalTxBytes(); long totalRxBytes = TrafficStats.getTotalRxBytes(); // to get mobile data count, subtract old from current long currentMobileSent = currentMobileTxBytes