Android AlarmManager setExact() is not exact

后端 未结 6 809
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 04:30

I need to plan sheduled task every 10 minutes.

As in Lollipop and higher version setRepeating() is inexact, I use setExact() and (on alarm

6条回答
  •  醉梦人生
    2020-12-01 05:12

    To answer the question on the system alarm...

    Android's stock Alarm Clock/Desk Clock app uses a combination of setAlarmClock and setExactAndAllowWhileIdle.

    The following code is used to update notifications:

    final PendingIntent operation = PendingIntent.getBroadcast(context, 0, 
        AlarmStateManager.createIndicatorIntent(context), flags);
    final AlarmClockInfo info = new AlarmClockInfo(alarmTime, viewIntent);
    
    alarmManager.setAlarmClock(info, operation);
    


    While at the same time the following code is used to schedule the actual alarm:

    if (Utils.isMOrLater()) {
        // Ensure the alarm fires even if the device is dozing.
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
    } else {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent)
    }
    


    The Pending intent set in setExactAndAllowWhileIdle triggers the alarm while setAlarmClock's intent is then simply ignored.


    Android Googlesource

提交回复
热议问题