0

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)?

2
  • IIRC the minimum amount of time at which you can expect it to be accurate is 10 minutes on Android 5.1 and up. I don't remember the exact number though
    – Zoe is on strike
    Sep 22, 2018 at 10:12
  • Thanks! That would explain. Trying to confirm long term times now. Like 12 hours. Sep 22, 2018 at 14:23

1 Answer 1

0

Found what I was doing wrong. I was calling set (and all other variations) with alarm type RTC_WAKEUP and passing it 300000 (5 mins). I didn't realize that the alarm is expecting time of when it should trigger in milliseconds. I kept thinking that it was number of milliseconds to wait from now. So before it was:

alarmManager.set(AlarmManager.RTC_WAKEUP, milliseconds, pendingIntent)

and the fix was:

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + milliseconds, pendingIntent)

Mind you. This doesn't mean that I got it to work well for small intervals. I got it to work in general - even for large intervals.

PS. I don't know if this is useful for stackoverflow to keep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.