I need to get the amount of data i.e. internet usage over periods of one month.
I have been doing some research and found the TrafficStats class. However, it seems confusing. What is the difference between getUidRxBytes() and getUidRxPackets().
How can I get the data used over a period of one month?
Use the library android.net.TrafficStats to get the necessary information. You'll have to build an algorithm to calculate that the use of average.
Example:
long appDataUsage = android.net.TrafficStats.getUidRxBytes(UID);
long total = android.net.TrafficStats.getTotalRxBytes();
Related
I need to get the approximately precise data count for Rx and Tx between two timestamps. The granularity should be here minutes, e.g daily summaries are not sufficient. Is this information logged in Android? I first thought about implementing a VpnService to handle this, but I also found NetworkStatsManager#querySummaryForDevice. Can this method summarize the data usage to my needs? How does Android store internally the data consumption? If it is just a summed up counter, this would not fit my purpose.
I am trying to get the exact Data Usage per Day , or by Range of Time in Android
however from what I searched, I cannot found anything that could do this
Something that I tried
TrafficStats
this one will reset all data every time the device is boot, so I
couldn't use it
NetworkPolicyManager
this one require system permission. So, I can't use it
any help would be appreciate
You need to have a service that will periodically call TrafficStats APIs to get current statistics, and store the results. E.g. the delta between two invocations of getMobileTxBytes() is the number of bytes received during that time period.
Of course, if someone just pulls a battery out of their phone, you'll lose statistics since the last invocation of your service. So set the frequency of updates according to your needs (every hour versus every minute).
I'm developing an application that measure the data traffic recived through mobile data interface (no wifi) from all processes. Additionally this counter have to be related to a date range, i.e. betheen March 1 and April 1.
I had read about TrafficStats class, but in the documentation doesn't mentioned any about from when are the stats.
This is my first question and I really appreciate your help.
Thanks
I had read about TrafficStats class, but in the documentation doesn't mentioned any about from when are the stats.
"From when" should not matter to you. Take a reading at a point in time, take another reading at a later point in time, and the difference between the two is the bandwidth consumed between those two points in time.
Additionally this counter have to be related to a date range, i.e. betheen March 1 and April 1.
You would need to handle this yourself, most likely, checking for the amount of bandwidth consumption every so often (e.g., every 4 hours via AlarmManager), storing the results in a database, and then using that information to determine the bandwidth consumed over extended periods of time.
I'm wondering which is the best way to store a time into a database.
Is it better to store the time in millis as a LONG value or use the sql DATE type.
I want to store a time into the SQLite database of the android device and send it later to a server. Which is the best way in consideration of performance.
I've encountered both methods and both are valid options. Personally I would go with the sql DATE method since you have many predefined methods with reading/manipulating/writing dates in SQLite, as explained here. Storing time in millis is fine if you're willing to run extra calculations to verify the accuracy of the data later on, coming from a QA perspective.
Depends entirely what you are going to do with the time. If you are just going to display it, a text field is perfectly fine. If you are going to convert it do different formats, a long is probably the way to go.
As everyone else is saying, it all depends on what you want to do with it.
Considering your situation, I would store it in whatever format the server you are going to send it to needs.
As the saying goes, there are many ways to skin this particular cat. For example: in some instances I've taken the date and converted it to an int in the format of yyyymmdd, that way I can do simple less than/greater than/equal to comparisons without having to resort to the different time functions in SQL.
I would like to get some help about getting 3G data statistics between a date interval.
As far as I know, I should use TrafficStats (Android api 2.2 or higher). I would like to save this information into a SQLite table to show statistics for apps monthly:
Interval date: 01/01/2012 - 31/01/2012
Google Maps - 1,5 Mb
Google Talk - 0,9 Mb
Facebook app - 5,6 Mb
So, I Think I should use a background service. Is this the best way? How should I try to do it in the background service? How do you think I should save the information in SQLite?
(#Pabloku, sorry this answer is coming so late, hopefully it will still be of some help)
Firstly, if you're looking to get traffic stats for individual apps, but only on 3G, it's not possible using public APIs. Android provides the TrafficStats.getUidTxBytes(int) and TrafficStats.getUidRxBytes(int) as public APIs to get the total number of bytes used by apps, but nothing (public) to separate them by interface.
Assuming this doesn't ruin your day, here is a pseudocode algorithm for how to do the rest of what you mention:
Set an alarm if necessary (using AlarmManager) for the start of your range, and store these values (presumably in a DB). Reason: you may need to subtract these existing TrafficStats values as an offset if they are > 0 at the time your date range starts.
Also set an alarm for the end of your range.
Create a BroadcastReceiver to receive ACTION_SHUTDOWN.
In your BroacastReceiver, note down the TrafficStats for your app(s) at shutdown. Reason: TrafficStats will get reset on every reboot.
If this is the first shutdown since start: subtract your initial offset and store that final value (being careful to remove the initial offset)
Otherwise, whatever value is reported will be accurate since boot.
Once your end alarm is triggered, note down the TrafficStats at that point, and add all previously collected stats
(if somehow the phone never rebooted between start and end, just do endStats - startStats).
Good luck!