I have searched a lot about this. Found the same code everywhere which solves the purpose partially. As API documentation says, it reset the counter once the device restarts. Sometimes the counter just resets even without the restart. Below is the code
float totalRxBytes = (float)TrafficStats.getTotalRxBytes()/(float)1048576; // Received
float totalTxBytes = (float)TrafficStats.getTotalTxBytes()/(float)1048576; // Sent
float mobRxBytes = (float)TrafficStats.getMobileRxBytes()/(float)1048576;
float mobTxBytes = (float)TrafficStats.getMobileTxBytes()/(float)1048576;
float wifiRxBytes = totalRxBytes - mobRxBytes;
float wifiTxBytes = totalTxBytes - mobTxBytes;
But I could not figure out any way to have this data from a specific date OR for a month? Please help. Any pointer will be appreciated. Thanks.
Check Android Phone Data Use To view your data usage, tap Settings > Data. You can Set mobile data limit on this screen. For more detail, tap Settings > Connections > Data usage. Swipe up to see how much data your apps use, ordered from most to least.
Checking how much data you've used on AndroidOpen the Settings app. Tap on Connections. Tap Data Usage.
Restrict background data usage by app (Android 7.0 & lower) To view and restrict background data usage by app: Open your phone's Settings app. Data usage. Tap Mobile data usage.
First of all, TrafficStats.getTotalRxPackets():
Return number of packets received since device boot.
The same is with TrafficStats.getTotalTxPackets()
This class is not helpful for getting month statistics.
I would advise solution working from API 23:
NetworkStatsManager
This class has the possibility to obtain statistics per device or per package. Especially helpful for you would be function:
NetworkStatsManager.querySummaryForDevice()
which expect measuring start time as third parameter and end time as fourth paramter.
The sample project can be found here. It shows how to obtain access to NetworkStatsManager asking for appropriate runtime permissions.
This solution is only API 23+.
If you really want to use TrafficStats then create a Service that would get the result of TrafficStats.getTotalRxPackets() every hour, count the difference, save it in database in different row per day.
I agree with R. Zagorski, but I have a different approach in mind.
Use TrafficStats to get amount of packets received/sent, subtract the last amount from it and then save it using SharedPreferences along with the last amount. Now, to handle a device restart, always check if the last count is greater than current amount. If yes, reset the last amount to 0. Also, keep track of when the month started. As soon the month has ended, don't forget to reset the counts to 0!
If you wish to keep track of the previous month counts as well, use a List. When the month gets over, add the total amount to the array at the index of the month number. Also, keep in mind that the first index is 0, not 1. So, you'll have to offset the array by 1 value to be able to directly use the month number to query your list.
This has an advantage over R. Zagorski's idea (which is also quite good) that it can be used from api level 8 as that's the minimum for TrafficStats.
Hope I Helped :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With