问题
I am implementing an application that has a minimum API level 14 (this is important) and requires consistent interval measuring. No need for ms precision, it just needs to be always counting the time (elapsed seconds).
So far, to treat time intervals, I knew these solutions:
System.nanoTime()
- Works great if Android is running, but stops on deep sleep (this is bad).System.currentTimeMillis()
- Good, but not appropriate because it can be changed by the user or by code withsetCurrentTimeMillis(long)
.SystemClock.elapsedRealtimeNanos()
- Counts elapsed time even on deep sleep, but requires API level 17.
Is there another way to get a reasonable second-precision interval measuring on API 14?
I thought about using System.currentTimeMillis()
listening to the broadcast intents to see if the system clock has changed. However this does not seem to be the best solution.
回答1:
Have you tried SystemClock.elapsedRealtime() ?
来源:https://stackoverflow.com/questions/26266441/android-time-intervals-with-deep-sleep-system-nanotime-system-currenttimemi