I need to measure long elapsed time on Android and there may be device reboots in between.
From what I've understand, System.nanoTime()
is resetted every time the device reboot, and System.currentTimeMillis()
is unreliable because user can change it.
The only solution that I came up with is to listen to ACTION_SHUTDOWN
and BOOT_COMPLETED
, use System.currentTimeMillisec()
to calculate the elapsed time (user can't change clock time while the device is off, hopefully :) ) and add it to the last System.nanoTime()
I had before shutting down.
I honestly don't like this solution because it's very expensive (I need to listen to 2 broadcast events) and inaccurate, but I couldn't figure out any other way to do this.
Any ideas? Also a native solution would be good for me.
You can get around the user changing time by using an internet time server to get the times when you check. There are a couple of ways to do this.
Date
header )If you simply persist this value then the user can do nothing to mess up your calculation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With