title: Android: AlarmManager Cannot Cancel Alarms
body: <p>I am using the alarmManager to set up multiple alarms within my application and have run into an issue. When setting the PendingIntent this way:</p>
  
  <pre><code>timerAlarmIntent = PendingIntent.getBroadcast(myContext, 0, alarmIntent, 0);
  myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
  </code></pre>
  
  <p>Where the requestCode is set to 0 (beside the context in getBroadcast) I can use:</p>
  
  <pre><code>myAM.cancel(timerAlarmIntent);
  </code></pre>
  
  <p>to cancel any amount of alarms I set in the application. However, each alarm I set overwrites the last so only one alarm ends up going off. If I set the requestCode to a unique number such as:</p>
  
  <pre><code>timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
  myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
  </code></pre>
  
  <p>Where i is set to a unique id number for each alarm using a for loop. This way all alarms run fine, but I cannot cancel the alarms because only one alarm will actually cancel when I run:</p>
  
  <pre><code>myAM.cancel(timerAlarmIntent);
  </code></pre>
  
  <p>How do I set the requestCode to a unique number and also have the option to cancel it?</p>
  
  <p>The code I ended up using to solve the problem is:</p>
  
  <pre><code>timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
  ArrayList&lt;PendingIntent&gt; intentArray = new ArrayList&lt;PendingIntent&gt;();
  intentArray.add(timerAlarmIntent);
  myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
  </code></pre>
  
  <p>To cancel the PendingIntents I set up the following method:</p>
  
  <pre><code>    private void cancelAlarms(){
      if(intentArray.size()&gt;0){
          for(int i=0; i&lt;intentArray.size(); i++){
              myAM.cancel(intentArray.get(i));
          }
          intentArray.clear();
      }
  }
  </code></pre>