'setAlarmClock() is not exact and system adjusts the time for it

Android documentation for setAlarmClock:

Invoke an alarm at a precise time in the future. Because these alarms are highly visible to users, the system never adjusts their delivery time. The system identifies these alarms as the most critical ones and leaves low-power modes if necessary to deliver the alarms.

But it looks like system does adjust delivery time of alarm.

Code(Kotlin):

val intent = Intent(context, AlarmService::class.java)
val pendingIntent = PendingIntent.getService(context, 0, intent, 0)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

alarmService.setAlarmClock(AlarmManager.AlarmClockInfo(epochMillis, null), intent)

The alarm was set for a minute, but system adjusted it. It must be invoked in 20:34:26, but actually it will be invoked in 20:45:28

Dump ('adb shell dumpsys alarm' command on Android 8.1.0 device):

Batch{68ad18a num=1 start=1468562132 end=1468562132 flgs=0x3}:
    RTC_WAKEUP #0: Alarm{faf73fb type 0 when 1652550328316 io.pelmenstar.testalarm}
      tag=*walarm*:io.pelmenstar.testalarm.START_ALARM
      type=0 whenElapsed=+11m58s237ms when=2022-05-14 20:45:28
      window=0 repeatInterval=0 count=0 flags=0x3
      Alarm clock:
        triggerTime=2022-05-14 20:34:26
        showIntent=null
      operation=PendingIntent{71cf62a: PendingIntentRecord{8d1cc1b io.pelmenstar.testalarm.startService}}

I partially resolved the problem. If go to the App Power-saver manager (Settings -> Battery -> App power-saver manager -> Some application) and disable 'Scheduled background wake-up', the system really do not adjust alarms of this application.

The question is how to resolve the problem without disabling 'Scheduled background wake-up' option or how to disable this option in runtime or install time (without user interaction). I noticed that on some apps (like Facebook) this option is disabled, but I didn't do that myself.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source