2016年3月24日 星期四

Difference between setRepeating and setInexactRepeating of AlarmManager

Reference:

http://stackoverflow.com/questions/21232984/difference-between-setrepeating-and-setinexactrepeating-of-alarmmanager


Example:

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, alarmIntent);

There are two differences between these calls. The simpler one is that the intent will be sent every fifteen minutes on the first call, and every day on the second call (as you can see in the third parameter). The more complicated difference is the function call itself: setRepeating will schedule the first alarm for exactly every fifteen minutes; setInexactRepeating will schedule the second alarm for approximately every 24 hours, meaning it might deviate from that interval - with the advantage of consuming less power.

Illustration:

setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

setRepeating在第一次啟動 alarm後,會每隔 intervalMillis啟動一次alarm
setInexactRepeating則是第一次啟動 alarm後,會每隔大約 intervalMillis啟動一次alarm,目的是為了省電,啟動時間相近的 alarm會同時啟動

沒有留言: