【发布时间】:2017-03-19 10:42:54
【问题描述】:
为什么 AlarmManager 有时会在 5 分钟后执行失败,这是应该的?例如,有一次它可能不起作用,但在下一个周期它会工作 2 次(或 3 次)。甚至滞后 1 分钟。
API 17 (Android 4.2.1) - setRepeating 应该是准确的。
public void scheduleAlarm()
{
Intent intent = new Intent(getApplicationContext(), TestAlarmReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, TestAlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 5 * 60 * 1000, pIntent); // every 5 minutes
Log.i(TAG_LOG, "The alarm started");
}
日志:
<...>
17.03.2017 07:20:30
17.03.2017 07:25:30
17.03.2017 07:30:30
17.03.2017 07:40:25 <- this
17.03.2017 07:40:30 <- this
17.03.2017 07:45:30
17.03.2017 07:50:30
17.03.2017 07:55:30
17.03.2017 08:00:30
17.03.2017 08:10:25 <- this
17.03.2017 08:10:30 <- this
17.03.2017 08:15:30
17.03.2017 08:20:30
<...>
17.03.2017 16:50:30
17.03.2017 16:55:30
17.03.2017 17:01:10 <- this
17.03.2017 17:05:30
17.03.2017 17:10:30
17.03.2017 17:15:30
17.03.2017 17:21:08 <- this
17.03.2017 17:25:30
17.03.2017 17:40:29 <- this
17.03.2017 17:40:29 <- this
17.03.2017 17:40:30 <- this
17.03.2017 17:45:30
17.03.2017 17:51:09 <- this
17.03.2017 17:55:30
17.03.2017 18:00:50
<...>
【问题讨论】:
标签: android alarmmanager repeatingalarm