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.
System.currentTimeMillisec()
based on external time sources (possibly NTP, GSM, GPS, ?) - no user intervention required and probably at boot (i.e. beforeBOOT_COMPLETED
). You could use an external time service as well but that needs network access. – Phila