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.
First of all, TrafficStats.getTotalRxPackets():
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.