private static final void rescheduleMissedAlarms(ContentResolver cr,Context context,AlarmManagerInterface manager){
  long now=System.currentTimeMillis();
  long ancient=now - DateUtils.DAY_IN_MILLIS;
  String[] projection=new String[]{CalendarContract.CalendarAlerts.ALARM_TIME};
  if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(context,Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
    Log.d(TAG,"Manifest.permission.READ_CALENDAR is not granted");
    return;
  }
  Cursor cursor=cr.query(CalendarAlerts.CONTENT_URI,projection,WHERE_RESCHEDULE_MISSED_ALARMS,(new String[]{Long.toString(now),Long.toString(ancient),Long.toString(now)}),SORT_ORDER_ALARMTIME_ASC);
  if (cursor == null) {
    return;
  }
  if (DEBUG) {
    Log.d(TAG,"missed alarms found: " + cursor.getCount());
  }
  try {
    long alarmTime=-1;
    while (cursor.moveToNext()) {
      long newAlarmTime=cursor.getLong(0);
      if (alarmTime != newAlarmTime) {
        if (DEBUG) {
          Log.w(TAG,"rescheduling missed alarm. alarmTime: " + newAlarmTime);
        }
        AlertUtils.scheduleAlarm(context,manager,newAlarmTime);
        alarmTime=newAlarmTime;
      }
    }
  }
  finally {
    cursor.close();
  }
}

private static final void rescheduleMissedAlarms ( ContentResolver cr , Context context , AlarmManagerInterface manager ) { long now = System . currentTimeMillis ( ) ; long ancient = now - DateUtils . DAY_IN_MILLIS ; String [ ] projection = new String [ ] { CalendarContract . CalendarAlerts . ALARM_TIME } ; if ( Build . VERSION . SDK_INT >= 23 && ContextCompat . checkSelfPermission ( context , Manifest . permission . READ_CALENDAR ) != PackageManager . PERMISSION_GRANTED ) { Log . d ( TAG , "Manifest.permission.READ_CALENDAR is not granted" ) ; return ; } Cursor cursor = cr . query ( CalendarAlerts . CONTENT_URI , projection , WHERE_RESCHEDULE_MISSED_ALARMS , ( new String [ ] { Long . toString ( now ) , Long . toString ( ancient ) , Long . toString ( now ) } ) , SORT_ORDER_ALARMTIME_ASC ) ; if ( cursor == null ) { return ; } if ( DEBUG ) { Log . d ( TAG , "missed alarms found: " + cursor . getCount ( ) ) ; } try { long alarmTime = - 1 ; while ( cursor . moveToNext ( ) ) { long newAlarmTime = cursor . getLong ( 0 ) ; if ( alarmTime != newAlarmTime ) { if ( DEBUG ) { Log . w ( TAG , "rescheduling missed alarm. alarmTime: " + newAlarmTime ) ; } AlertUtils . scheduleAlarm ( context , manager , newAlarmTime ) ; alarmTime = newAlarmTime ; } } } finally { cursor . close ( ) ; } }
searches the calendaralerts table for alarms that should have fired but have not and then reschedules them . this method can be called at boot time to restore alarms that may have been lost due to a phone reboot .
