0
爲什麼AlarmManager有時無法正好5分鐘後執行,因爲它應該是?例如,一次它可能不起作用,但在下一個時期它工作2(或3)次。甚至滯後1分鐘。爲什麼AlarmManager在設備上沒有完全重複?
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
<...>
是的,但我提到我有** API 17 **。沒有'setExact()'。 –