There is a code which supposed to trigger an action at a specified precise time using the AlarmManager
(the next day at 7:00am):
val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, FooIntentService::class.java)
val pendingIntent = PendingIntent.getService(this, 0, intent, 0)
// Set alarm
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, 7)
calendar.set(Calendar.MINUTE, 0)
// Set tomorrow
calendar.add(Calendar.DATE, 1)
manager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
I have tested this code to trigger event in up to 5 minutes from now, closing the app (close all apps) and putting it on sleep (pressing Hold button) - and it works. Yet when I set the time for tomorrow at 7:00am (which is way more then 5 minutes from now) - it would never trigger, until I have unblocked it (woke up manually). At the moment I woke it up - the action triggered right away.
Question: is the example code I have provided correct for setting planned event in my case?