Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the GPRS Mobile data usage for 1 month?

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.

like image 560
Bharat Avatar asked Oct 14 '16 09:10

Bharat


People also ask

How do I check my monthly data usage?

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.

How much data do I have left on my phone for this month?

Checking how much data you've used on AndroidOpen the Settings app. Tap on Connections. Tap Data Usage.

Where is data usage in settings?

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.


2 Answers

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.

like image 199
R. Zagórski Avatar answered Oct 17 '22 15:10

R. Zagórski


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

like image 22
Ishan Manchanda Avatar answered Oct 17 '22 17:10

Ishan Manchanda