简体   繁体   English

Android:深度睡眠的时间间隔(System.nanoTime(),System.currentTimeMillis(),SystemClock.elapsedRealtimeNanos())

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

I am implementing an application that has a minimum API level 14 (this is important) and requires consistent interval measuring. 我正在实现一个具有最低API级别14的应用程序(这很重要)并且需要一致的间隔测量。 No need for ms precision, it just needs to be always counting the time (elapsed seconds). 不需要ms精度,它只需要总是计算时间(经过的秒数)。

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). System.nanoTime() - 如果Android正在运行,效果很好,但在深度睡眠时停止(这很糟糕)。
  2. System.currentTimeMillis() - Good, but not appropriate because it can be changed by the user or by code with setCurrentTimeMillis(long) . System.currentTimeMillis() - 很好,但不合适,因为它可以由用户或使用setCurrentTimeMillis(long)代码更改。
  3. SystemClock.elapsedRealtimeNanos() - Counts elapsed time even on deep sleep, but requires API level 17. SystemClock.elapsedRealtimeNanos() - 计算即使在深度睡眠时经过的时间,但需要API级别17。

Is there another way to get a reasonable second-precision interval measuring on API 14? 还有另一种方法可以在API 14上获得合理的二次精度间隔测量吗?
I thought about using System.currentTimeMillis() listening to the broadcast intents to see if the system clock has changed. 我想过使用System.currentTimeMillis()监听广播意图来查看系统时钟是否已经改变。 However this does not seem to be the best solution. 然而,这似乎不是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.nanoTime 和 System.currentTimeMillis() - System.nanoTime and System.currentTimeMillis() System.currentTimeMillis 与 System.nanoTime - System.currentTimeMillis vs System.nanoTime System.nanoTime与System.currentTimeMillis - System.nanoTime vs System.currentTimeMillis Java计时,System.nanoTime()比System.currentTimeMillis()更糟糕,但它是否会在睡眠中持续存在? - Java timing, System.nanoTime() batter than System.currentTimeMillis() but does it persist over sleep? 将System.currentTimeMillis()与System.nanoTime()结合使用是否合理? - Is it reasonable to use System.currentTimeMillis() in conjunction with System.nanoTime()? System.nanoTime / System.currentTimeMillis = 107(应该为1e6吗?) - System.nanoTime / System.currentTimeMillis = 107 (should this be 1e6 ?) 为什么System.nanoTime()和System.currentTimeMillis()如此迅速地分开? - Why do System.nanoTime() and System.currentTimeMillis() drift apart so rapidly? Java System.nanoTime()与System.currentTimeMillis()。 为什么它们有不同的输出? - Java System.nanoTime() vs System.currentTimeMillis(). Why do they have different outputs? 为什么 System.nanoTime() 比 System.currentTimeMillis() 慢(在性能上)? - Why is System.nanoTime() way slower (in performance) than System.currentTimeMillis()? 反复调用System.currentTimeMillis()/ nanoTime() - Repeatedly Calling System.currentTimeMillis()/nanoTime()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM