Android: time intervals with deep sleep (System.nanoTime(), System.currentTimeMillis(), SystemClock.elapsedRealtimeNanos())

时光毁灭记忆、已成空白 提交于 2019-12-12 11:38:32

问题


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:

  1. System.nanoTime() - Works great if Android is running, but stops on deep sleep (this is bad).
  2. System.currentTimeMillis() - Good, but not appropriate because it can be changed by the user or by code with setCurrentTimeMillis(long).
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!