I am currently trying to measure the bandwidth consumed by a piece of code in android. Whenever I am calling the networkStatsManager.querySummaryForDevice(), it is returning data value that is same for a particular period of time.
Here is the code sample:
networkStatsManager.querySummaryForDevice(
ConnectivityManager.TYPE_WIFI,
"",
0,
System.currentTimeMillis()
)
Can anyone confirm that after how much time the latest data is updated in NetworkStatsManager?
Related
The problem:
I need to synchronize some data set time-stamped using System.nanoTime() with another one time-stamped using SensorEvent.timestamp.
Description:
I am working on System Identification of a quadcopter. Data acquisition is done using Android API. The input to my system is the pulse width modulation (PWM) which are time-stamped using System.nanoTime() with a frequency of 200 Hz.
The output of the system is Androids's sensors measurements which are time-stamped using SensorEvent.timestamp.
I've checked many online resources but unfortunately didn't help. Can I access SensorEvent.timestamp whenever I want so I time-stamp the inputs using this clock?
I'm afraid this problem is a bit complicated. SensorEvent.timestamp switched from being System.nanoTime() to SystemClock.elapsedRealtimeNanos() at some point, but it is unclear when that happened in terms of devices and API levels. In fact, it appears to be completely manufacturer-dependent: https://code.google.com/p/android/issues/detail?id=56561
Furthermore, these clocks count relative to different events or points in time, and nanoTime() does not necessarily continue counting in deep sleep mode; Android: time intervals with deep sleep (System.nanoTime(), System.currentTimeMillis(), SystemClock.elapsedRealtimeNanos())
The only way we found to solve the problem is to periodically record System.currentTimeMillis(), System.nanoTime() and SystemClock.elapsedRealtimeNanos() as atomically as possible, and use the offset between SensorEvent.timestamp and both System.nanoTime() and SystemClock.elapsedRealtimeNanos() to figure out which one the sensor is using. Once you have that you can get an absolute timestamp for each sensor event adding the delta between your SensorEvent.timestamp and System.nanoTime() or SystemClock.elapsedRealtimeNanos() (respectively) to your System.currentTimeMillis(). It's not perfect, and could be off by as much as a few millisecond, but it's as good as we could get it.
Hope that helps!
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).
This is more of a "is this valid" type of question than "how do I do it", since my coworkers want a second opinion on it. We want to have an accurate timestamp of the time user takes a picture using our application, and the stamp should be something that our users can't influence. Since we want our application to work even though there's no internet connection, just asking the time from a server won't work.
I've currently done this so that the application asks a timestamp from the server, and at the same time takes note of what time Android's elapsedRealtime() clock has at that point. When user takes a picture, the software checks the elapsedRealtime() again, and then calculates the timespan between the two points of time saved from elapsedRealtime(). That timespan is then added to the timestamp gotten from the server, so that we'd have the time that represents the user's current time.
In the abstract level, does this sound like a valid solution that gives accurate time everytime? Are there other solutions to do this?
EDIT:
A requirement I forgot to mention: the pictures can't be lost even if the phone is turned off before sending them to the server, nor should the timestamp lose validity because of that.
Thanks in advance,
Xevas
If it is absolutely imperative to get this time correct, you could start a timer when the user takes the first picture and then when you get internet access, check time on the server and calculate the time when the picture was taken by subtracting the time elapsed since the timer was turned on from the current time.
I've noticed that System.currentTimeMillis() time is device dependent. If I change the time on the device's clock, this method will return a different answer.
For example: If the real time now is 10:00, and I change the clock on my device to 9:30, then System.currentTimeMillis() will return the 9:30 time (in milliseconds..).
I've also tried this answer and some other answers, but didn't find anything useful.
I should state that my app works mostly offline.
Is there a way to get the real current time (device independent) without external API?
If it were not for the 'offline' part, I'd have suggested to use a time server, but given that your app is offline most of the time that might not be a good solution.
If you don't need the actual time but just a time that cannot be messed with, you can use SystemClock.elapsedRealtime() which gives you the time since the device last booted.
You could also combine time server and SystemClock.elapsedRealtime(): Fetch the time from timer server once (e.g. after bootup) and from then on add elapsedRealtime() to that initial value (minus the elapsedRealtime value of when you get the timerserver value).
If you use the GPS location provider, getTime() will return the UTC time derived from the GPS signal, rather than the device time. The GPS location provider can work offline - but it will be much slower to obtain a fix compared to being online when it can access the A-GPS info.
My application is time dependent and I don't want change in device date and time affect my application, like if user deliberately set device date to any previous date . Is there any way to get current date and time when user is connected to mobile network or WiFi I don't want to use GPS.
Javadoc of the SystemClock class describe different ways of counting elapsed time for various scenarios and conditions.
In your case you have to use
long time = SystemClock.elapsedRealtime();
Return the time since the system was booted, and include deep sleep.
This clock is guaranteed to be monotonic, and continues to tick even
when the CPU is in power saving modes, so is the recommend basis for
general purpose interval timing.
Call it for the first time when you want to start tracking the use of Network (I assume you already know how to do it) and store that value.
When you receive event about user not using network anymore, call the same method again and calculate spent time.
long elapsed = time - SystemClock.elapsedRealtime();
Next you can transform milliseconds to String for example like this:
String formattedElapsedTime = DateUtils.formatElapsedTime(elapsed/1000);//note that this method takes second as arguments so we have to divide it by 1000
You should call webservice and get current date and time of server. Then server would be responsible for the actual date/time. Best way is to build your own webservice, because it gives you more control than using third party.