First of all I'm aware of other questions about this such as: scheduling alarm for every second in android 5.1 or How to use Android AlarmManager with small intervals like 1 minute?
So it basically says that this behavior with small intervals is as intended because of system optimizations. But what is small? It was talking about setting alarms for every several seconds. But what about minutes? Same problem? What intervals will start behave closer to intended times? Hours? Days? And these seem to talk about setting repeating intervals using setRepeating.
So here is what I'm struggling with. I'm trying to setup alarms for every 5 min (300000 milliseconds) is this still too small? And I'm trying to do it via set or even setExact or setWindow. After the first alarm triggers I setup new one for 300000 ms. Here's how I setup initially:
//initially somewhere I call:
IntervalScheduler.createAlarm(context!!, 300000)
...
//which is this:
object IntervalScheduler
{
fun createAlarm(ctx: Context, milliseconds: Long)
{
val intent = Intent(ctx, AlarmReceiver::class.java)
val alarmManager = ctx.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
if (Build.VERSION.SDK_INT >= KITKAT)
{
// doesn't help
//alarmManager.setExact(AlarmManager.RTC_WAKEUP, milliseconds, 60000, pendingIntent)
// this doesn't help as well - trying to give it a window of 1 minute
alarmManager.setWindow(AlarmManager.RTC_WAKEUP, milliseconds, 60000, pendingIntent) // doesn't help
}
else
{
alarmManager.set(AlarmManager.RTC_WAKEUP, milliseconds, pendingIntent)
}
}
}
// this gets called fine
class AlarmReceiver: BroadcastReceiver()
{
override fun onReceive(context: Context?, intent: Intent?)
{
Log.i("NOTE","Notifying")
// create next alarm
IntervalScheduler.createAlarm(context!!, 300000)
}
}
Here is resulting log:
09-21 22:21:53.846 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:22:54.812 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:23:55.023 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:24:00.655 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:24:06.085 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:24:11.616 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:24:17.193 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:24:22.674 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:24:28.164 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:24:33.852 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:25:34.815 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:26:57.604 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:28:14.842 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:29:14.968 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:30:15.275 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:31:15.459 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:32:15.644 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:33:15.834 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:34:15.941 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:35:16.143 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:36:17.804 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:37:17.969 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:37:23.686 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:37:29.131 26982-26982/com.sykes I/NOTE: Notifying
09-21 22:37:34.749 26982-26982/com.sykes I/NOTE: Notifying
It's not anywhere near 5 mins. In fact it's rarely more than a minute. Like last 4 calls happened within the same minute. System optimizes 5 minutes down to 6-5 seconds sometimes?
Is this still as expected? Would it be much closer to expectation if set to 3600000 (1 hour) or 86400000 (24 hours)?