Skip to content

DeviceState: Use AlarmManager for time schedule#84

Merged
chenxiaolong merged 1 commit intomasterfrom
realtime
Apr 7, 2026
Merged

DeviceState: Use AlarmManager for time schedule#84
chenxiaolong merged 1 commit intomasterfrom
realtime

Conversation

@chenxiaolong
Copy link
Copy Markdown
Owner

Previously, the timers were based on the SystemClock.uptimeMillis() clock, which is monotonic, but pauses while the device is in deep sleep. For devices that enter deep sleep frequently, a time interval based on this clock can be significantly longer than the same interval based on wall time.

This commit changes the timer mechanism to use AlarmManager instead. There are a couple caveats here:

  • AlarmManager enforces that the execution time is at least 5 seconds into the future in AOSP, but Google and OEMs can override this at runtime via DeviceConfig. This is unlikely to matter much since users are generally going to be configuring intervals in the minutes or hours.

  • The intervals are no longer exact, even when the device is awake. AlarmManager.set() always tries to batch calls to avoid unnecessary wakeups. AlarmManager.setExact() is intentionally not used in this initial implementation. This can be revisited in the future if devices are scheduling wakeups too much later than what the user expects.

Fixes: #83

Previously, the timers were based on the SystemClock.uptimeMillis()
clock, which is monotonic, but pauses while the device is in deep sleep.
For devices that enter deep sleep frequently, a time interval based on
this clock can be significantly longer than the same interval based on
wall time.

This commit changes the timer mechanism to use AlarmManager instead.
There are a couple caveats here:

* AlarmManager enforces that the execution time is at least 5 seconds
  into the future in AOSP, but Google and OEMs can override this at
  runtime via DeviceConfig. This is unlikely to matter much since users
  are generally going to be configuring intervals in the minutes or
  hours.

* The intervals are no longer exact, even when the device is awake.
  AlarmManager.set() always tries to batch calls to avoid unnecessary
  wakeups. AlarmManager.setExact() is intentionally not used in this
  initial implementation. This can be revisited in the future if devices
  are scheduling wakeups too much later than what the user expects.

Fixes: #83

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
@chenxiaolong chenxiaolong merged commit 273e1f9 into master Apr 7, 2026
1 check passed
chenxiaolong added a commit that referenced this pull request Apr 7, 2026
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
@chenxiaolong chenxiaolong deleted the realtime branch April 7, 2026 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Time schedule syncs don't work when device idle

1 participant