private void resetAlarmDueTimesOnReboot ( ) { // As the device has been rebooted, we use clock time rather than elapsed time since // booting to set check whether we missed any alarms while the device was off and to // make sure the next alarm time isn't too far in the future (indicating the system clock // has been reset). // // We subtract the current time from the next expected alarm time and then do the // following checks: // * If it's less than zero, that means we missed an alarm when the device was off, so // we schedule a replication immediately by setting the last alarm time to a time // getIntervalInSeconds() ago. // * If it's more than getIntervalInSeconds() in the future, that means the system clock // has been reset since the last alarm was fired. Therefore, we check that the initial // interval for the alarm is no later than getIntervalInSeconds() after the // current time so we minimise the impact of the system clock being reset and will at most // have to wait for the normal interval time. // * Otherwise, the clock time for the alarm seems reasonable, but we still need to update // the SharedPreference for the elapsed time since boot at which the last alarm would have // fired as that currently refers to the time since boot from the previous boot of the // device. // // We don't actually setup the AlarmManager here as it is up to the subclass to determine // if all other conditions for the replication policy are met and determine whether to // restart replications after a reboot. setPeriodicReplicationEnabled ( false ) ; long initialInterval = getNextAlarmDueClockTime ( ) - System . currentTimeMillis ( ) ; if ( initialInterval < 0 ) { setLastAlarmTime ( getIntervalInSeconds ( ) * MILLISECONDS_IN_SECOND ) ; } else if ( initialInterval > getIntervalInSeconds ( ) * MILLISECONDS_IN_SECOND ) { setLastAlarmTime ( 0 ) ; } else { setLastAlarmTime ( ( getIntervalInSeconds ( ) * MILLISECONDS_IN_SECOND ) - initialInterval ) ; } }
Reset the alarm times stored in SharedPreferences following a reboot of the device . After a reboot the AlarmManager must be setup again so that periodic replications will occur following reboot .