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.
Related
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();
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 currently writing an Android app that will need to do things such as :
Retrieving sensors data such as
ActivityRecognition data, say every 20-30 seconds
Retrieving GPS data from time to time (e.g. when activity recognition sends that user is using its bike or car), so I'd say a few times per day max for average user. Frequency of GPS data retrieving could be every 5-10 seconds for example.
Of course, this data should be stored somewhere to be, later on, analysed by my app. The analyse part is not the problem here as I do not need a real-time calculation of any kind, so my actual concern is how to store the data efficiently.
So if we consider an average user that will generate about 5000 sensors data + 5000 GPS data :
How to best store this data ? Database ? 1 file per day ? I'd say database for performance issue and simplicity of use, but I'm not sure it's very good practice to open/close a database connection every 10/20 seconds to add just one line of data. Also, a journaled file (one per day) could be a good idea but I think this is pretty bad for a performance point of view, even using serialization ?
Will storing these 10000 data degrade battery life much more than just retrieving sensors (ActivityRecognition, GPS) data without storage ? I mean, it seems to me that it will be a bit overconsuming, but in the same time GPS is already using so much battery...
Is there another way to do that ?
Also thought of in-memory storage then every few minutes it could be put in hard storage (SQLite, files), but I'm not sure this is a good idea in terms of safely keeping the data...
Thanks in advance
Compared to the GPS battery consumption, the reading and writing in the database will consume almost nothing (it is flash storage, after all), so no worries there. The database would be the best option to store this data in my opinion and I don't see a problem in creating a single entry every 10 or 20 seconds.
Also thought of in-memory storage then every few minutes it could be
put in hard storage (SQLite, files), but I'm not sure this is a good
idea in terms of safely keeping the data...
This is a very good idea.
I have it done this way, and most systems that deal with files do it that way (called a buffer).
If your app crashes due a bug, some data will be lost, depending on the buffer size.
In all other cases (device will shut down, user terminates app) you have time to write (flush) the buffer.
Just an short additional note.
Other than shutting down peripherals, not really an option in your case, maximize the length of time the processor is inactive. (This is not the same as minimizing the length of time the process is active.) This allows the processor to drop into lower sleep states (called C-states). The deeper the sleep state, the more power savings.
In a general sense, this means
no polling; use interrupts instead,
if you need to periodically wake up to see if anything needs to
be done, make sure your interrupt period is the maximum allowable.
(Contrary to current practice, waking up every 10 ms does not
improve your responsiveness when the average event happens every 500
ms.)
This also applies to peripherals, as they drop into sleep states also when not active (Called D-states).
Minimize the number of cloud accesses you do.
Maximize the length of time between cloud accesses.
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.
Assume we have a lot of android devices that trying to submit real-time data(a time stamp and some numbers) to an django application.
What we need to do is get the mean value of those data that being submitted per second and constantly update an android device.
How can I achieve these goal?
Your best bet is to memcache the data then store it in the datastore. Your mention of "mean value of all these data per second" doesn't make much sense to me; unless you have a lot of users or are generating a battery-draining amount of data, the mean value per second will probably just be whatever data happened to be submitted that second. Also, "realtime" may have deceived you; it will take time for data to be transmitted to the server and back down to the phone. Especially on EDGE and slower data networks, it could cause a lag of a few seconds.
It may be practical to do per-minute mean calculations; to that end, I would simply do a memcache tuple of the number of values received that minute and the mean they represent. Then when you get a new value, recalculate the mean (multiply the mean by the number of values [not including the increment] and add the new value, then divide by the new number of values). Store this in memcache, and move on. It's unlikely your data will get evicted within that minute if you're using memcache wisely, and if it is evicted, you lost at most a minute of data. If that's unacceptable, back it up to the datastore.