Take a look at the TrafficStats class. To do this, you need to look at getTotalRxBytes () , getTotalTxBytes () , getMobileRxBytes () and getMobileTxBytes () .
Short review:
getTotalRxBytes = total downloaded bytes getTotalTxBytes = total uploaded bytes getMobileRxBytes = only mobile downloaded bytes getMobileTxBytes = only mobile uploaded bytes
So, to get only the number for the traffic associated with Wi-Fi, you only need to get the amount and subtract the mobile system as such:
getTotalRxBytes - getMobileRxBytes = only WiFi downloaded bytes getTotalTxBytes - getMobileTxBytes = only WiFi uploaded bytes
With the number of bytes, we can switch to different units, for example megabytes (MB):
getTotalRxBytes / 1048576 = total downloaded megabytes
As for getting use for an interval, such as a day, since these methods provide only the total (from the moment of loading), you will need to track the start number and then subtract it to get the number of bytes used during the interval. So, at the beginning of the day, for example, 12:00:00, you track the total usage:
startOfDay = getTotalRxBytes + getTotalTxBytes;
When the day ends, for example, 11:59:59 PM, you can then subtract two numbers and get the total usage for that day:
endOfDay = getTotalRxBytes + getTotalTxBytes; usageForDay = endOfDay - startOfDay;
So summary:
- use the methods provided by the TrafficStats class to get the total amount of Internet use.
- subtract mobile data from the total to get only Wi-Fi usage.
- convert bytes to any desired block using conversion factor
- save the amount of use at the beginning of the day, and then subtract the amount of use at the end of the day to get the amount used on that day.