Know if uptime timer is reset or Android has been rebooted

穿精又带淫゛_ 提交于 2019-12-11 07:23:54

问题


I have a independent clock created in my application. The clock runs as a different thread in the activity, starting from a base time set by me. I update the clock using the difference between the uptimemillis when I set the clock, and the current uptimemillis. But the uptimetimer, can be reset by Android, and is ever reset when Android reboot.

I only want to know if the uptime timer is reset, to know if the clock is still reliable.

How?


回答1:


According to the documentation you can use SystemClock.elapsedRealtime()

elapsedRealtime(): Returns milliseconds since boot, including time spent in sleep.

This value will only be reset when the device is restarted. Listen to the broadcast boot_complete and you will know when that is.

The problem with the updateMillis() is clearly noted in the documentation:

uptimeMillis(): Returns milliseconds since boot, not counting time spent in deep sleep. Note: This value may get reset occasionally (before it would otherwise wrap around).

From how I understand the documentation, by using elapsedRealtime your users cannot manipulate your counter.



来源:https://stackoverflow.com/questions/16255288/know-if-uptime-timer-is-reset-or-android-has-been-rebooted

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