1

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?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Eghes
  • 177
  • 1
  • 5
  • 18
  • 1
    `uptimeMillis()` is a `long`. It can handle 9,223,372,036,854,775,807 milliseconds before it would wrap around. That is a fairly long time, so unless you have evidence that it wraps around *significantly* before then, I would not worry about the problem. If you *do* have such evidence, I'd love to see it. – CommonsWare Apr 27 '13 at 18:27
  • 1
    Maybe you should just use the real time clock to get the absolute time instead of using the system uptime? As in: `new Date()` – le3th4x0rbot Apr 27 '13 at 18:39
  • @CommonsWare I read Android can reset it for internal reason... However uptime timer is reset at reboot. Therefore how i can "fix" this? – Eghes Apr 27 '13 at 19:28
  • @BaileyS If i use real time clock, if user change it, I cannot know it... – Eghes Apr 27 '13 at 19:28
  • 2
    "However uptime timer is reset at reboot" -- correct. "Therefore how i can "fix" this?" -- stop using `uptimeMillis()`, since you are assuming that it means anything other than the number of milliseconds since the last reboot. "If i use real time clock, if user change it, I cannot know it" -- listen for `ACTION_TIME_CHANGED` broadcasts. – CommonsWare Apr 27 '13 at 19:31
  • @Eghes, are you even sure that the uptime clock is immune to user real time clock changes? Is a user RTC time change really a major issue? – le3th4x0rbot Apr 27 '13 at 19:51
  • @CommonsWare Ok, if this broadcast work, I found the solution. – Eghes Apr 27 '13 at 20:01
  • @BaileyS Yes, the issue is I want at least know, when the clock is no more correct. Don't matter why, I want know when I must invalidate it. – Eghes Apr 27 '13 at 20:04
  • 1
    The broadcast looks like a great solution for that... http://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_CHANGED . Alternatively you might consider comparing the RTC to the Uptime clock... then you could at least know if only one of them changed, but not both. – le3th4x0rbot Apr 27 '13 at 20:10

1 Answers1

4

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.

Cremons
  • 990
  • 1
  • 9
  • 11