Android, how to find out the total number of Internet users per day via Wi-Fi and a mobile phone

How to find out the total amount of Internet data per day?

For example, at the end of the day I used 800mb, then it should return as "Internet usage of 800mb on May 20, 2015."

So how can I determine the overall data usage?

After long searches, I could only find the use of data when sending and receiving bytes, but not in general use.

And also want to share usage on Wi-Fi and mobile data.

+5
source share
1 answer

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.
+5
source

Source: https://habr.com/ru/post/1203356/


All Articles