why setRepeating() in AlarmManager is not showing alarm-notifications at exact time

44 Views Asked by At
            val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

            Log.e("TAG", "$hour : ${minute+1}: ", )
            val intent:Intent = Intent(this,MyReceiver::class.java)

          
            intent.putExtra("ALARM_TITLE","DRINK")
            intent.putExtra("ALARM_TIME","$hour : ${minute+1}")

            val calendar: Calendar = Calendar.getInstance()
            calendar.timeInMillis = System.currentTimeMillis()
            calendar.set(Calendar.HOUR_OF_DAY,hour)
            calendar.set(Calendar.MINUTE,minute+1)
            calendar.set(Calendar.SECOND,0)

            val pendingIntent:PendingIntent = PendingIntent.getBroadcast(
                this,
                0,
                intent,
                PendingIntent.FLAG_MUTABLE
            )

            alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                calendar.timeInMillis,
                AlarmManager.INTERVAL_DAY,
                pendingIntent,
            )

When I used setExact(), I received the notification at the exact specified time. However, when I switched to setRepeating(), the notification was delayed by 10 minutes, for instance, when I set the alarm for 10:01 PM, the notification arrived at 10:11 PM.

Please note that I require the alarms to be repeated daily as a reminder.

1

There are 1 best solutions below

0
Dimithrandir On

From the documentation for setRepeating():

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

In short, you cannot set exact repeating alarms since Android KitKat. Calling setRepeating() would have the same effect as calling setInexactRepeating().

However, you still have other options. As suggested in the documentation, you can set a one-time exact alarm for when you want it to first go off, and when it fires, you would set it up again with the same time value.