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

2

6

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.

Mcnutt answered 14/10, 2016 at 9:37 Comment(2)
any pointers please ?Mcnutt
you might find an answer here https://mcmap.net/q/1076951/-trafficstats-api-android-and-calculation-of-daily-data-usageRockwell
S
5

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.

Salinasalinas answered 18/10, 2016 at 7:11 Comment(3)
Hello Zag, Thanks for the information. I answer is useful and I am trying to implement the same.Mcnutt
I am facing 1 issue and could not solve it yet. ERROR : "java.lang.RuntimeException: java.lang.SecurityException: NetworkStats: Neither user 10053 nor current process has android.permission.READ_NETWORK_USAGE_HISTORY". I am implementing this in API 23 which is having a runtime security implementation for permission, I hope you understand what I am trying to say. I was able to add android.permission.READ_NETWORK_USAGE_HISTORY in Manifest but I am not able to add the same permission in Java file because its giving the compilation error. Could you please help ?Mcnutt
Look into the sample project. Especially into the AndroidManifest.xml and into MainActivity on how to ask for it and use it.Debatable
N
1

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

Neace answered 23/10, 2016 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.