title: AlarmManager - Creating multiple repeating alarm triggers on alarm creation, and
body: <p>I'm creating alarms in a for loop. Every time I create, delete, or update a Reminder in my app, I rerun scheduleAlarms() so that it recreates the alarms according to the new records. This seems to work well when I create a Reminder, but when I delete one, the alarm is still there. Maybe someone can help me figure out why my alarm isn't removed when I run scheduleAlarms() after removing the respective reminder from the list:</p>
  
  <pre><code>public static void scheduleAlarms(Context context) {
  
      AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  
      // Get all reminders from db and create alarms for them.
      ArrayList&lt;Reminder&gt; reminderList = (ArrayList&lt;Reminder&gt;)DbManager.getInstance().getAllReminders();
  
      for(Reminder reminder: reminderList) {
  
          Intent reminderServiceIntent = new Intent(context, ReminderService.class);
          reminderServiceIntent.putExtra(Constants.reminderBundleKey, reminder.getId());
  
          // Create unique pendingIntent for each reminder, request code is reminderId
          PendingIntent pendingIntent = PendingIntent.getService(context,reminder.getId(),reminderServiceIntent, 0);
  
          // Cancel the alarm before rescheduling
          try {
              alarmManager.cancel(pendingIntent);
          } catch (Exception e) {
              Log.e("rx", "PollReceiver: " + e.toString());
          }
  
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
          calendar.set(Calendar.MINUTE,reminder.getMinute());
  
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
              AlarmManager.INTERVAL_DAY, pendingIntent);
  
      }
  
  }
  </code></pre>
  
  <p>/** Update **/
  So instead of rescheduling everything after every update, I just broke the Receiver into a couple of methods: </p>
  
  <pre><code>public static void scheduleAlarm(Context context, Reminder reminder) {
  
          AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  
          Intent reminderServiceIntent = new Intent(context, ReminderService.class);
          reminderServiceIntent.putExtra(Constants.reminderBundleKey, reminder.getId());
  
          // Create unique pendingIntent for each reminder, request code is reminderId
          PendingIntent pendingIntent = PendingIntent.getService(context,reminder.getId(),reminderServiceIntent, 0);
  
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
          calendar.set(Calendar.MINUTE,reminder.getMinute());
  
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                  AlarmManager.INTERVAL_DAY, pendingIntent);
  
      }
  
      public static void cancelAlarm(Context context, Reminder reminder) {
  
          AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  
              Intent reminderServiceIntent = new Intent(context, ReminderService.class);
              PendingIntent pendingIntent = PendingIntent.getService(context,reminder.getId(),reminderServiceIntent, 0);
  
              try {
                  alarmManager.cancel(pendingIntent);
              } catch (Exception e) {
                  Log.e("rx", "PollReceiver: " + e.toString());
              }
      }
  </code></pre>