title: Android: How to create multiple alarms and cancel them after select
body: <p>I wanted to create a few number of alarms which has different times and canceled them one by one when I select each. I could create few alarms. But I have no idea how to cancel them when I select.</p>
  
  <p>Currently, alarm canceling process is working for the last alarm which I put. </p>
  
  <p>This is my work,</p>
  
  <pre><code>import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.app.TimePickerDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.TimePicker;
  
  import java.util.Calendar;
  
  public class MainActivity extends AppCompatActivity {
  
      TimePicker myTimePicker;
      Button buttonstartSetDialog;
      Button buttonCancelAlarm;
      TextView textAlarmPrompt;
  
      Button buttonstartSetDialogOne;
      Button buttonstartSetDialogTwo;
      Button buttonstartSetDialogThree;
  
      TextView textAlarmPromptOne;
      TextView textAlarmPromptTwo;
      TextView textAlarmPromptThree;
  
      TimePickerDialog timePickerDialog;
  
      int RQS_1 = 1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);
          textAlarmPromptOne = (TextView) findViewById(R.id.alarmpromptOne);
          textAlarmPromptTwo = (TextView) findViewById(R.id.alarmpromptTwo);
          textAlarmPromptThree = (TextView) findViewById(R.id.alarmpromptThree);
  
          buttonstartSetDialog = (Button) findViewById(R.id.startSetDialog);
          buttonstartSetDialog.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  textAlarmPrompt.setText("");
                  openTimePickerDialog(false);
  
              }
          });
  
          buttonstartSetDialogOne = (Button) findViewById(R.id.startSetDialogOne);
          buttonstartSetDialogOne.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  RQS_1=2;
                  textAlarmPromptOne.setText("");
                  openTimePickerDialog(false);
  
              }
          });
  
          buttonstartSetDialogTwo=(Button) findViewById(R.id.startSetDialogTwo);
          buttonstartSetDialogTwo.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  RQS_1=3;
                  textAlarmPromptTwo.setText("");
                  openTimePickerDialog(false);
  
              }
          });
  
          buttonstartSetDialogThree=(Button) findViewById(R.id.startSetDialogThree);
          buttonstartSetDialogThree.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  RQS_1=4;
                  textAlarmPromptThree.setText("");
                  openTimePickerDialog(false);
  
              }
          });
  
  
          buttonCancelAlarm = (Button) findViewById(R.id.cancel);
          buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  cancelAlarm(RQS_1);
              }
          });
      }
  
      private void openTimePickerDialog(boolean is24r) {
          Calendar calendar = Calendar.getInstance();
  
          timePickerDialog = new TimePickerDialog(
                  MainActivity.this,
                  onTimeSetListener,
                  calendar.get(Calendar.HOUR_OF_DAY),
                  calendar.get(Calendar.MINUTE),
                  is24r);
          timePickerDialog.setTitle("Set Alarm Time");
  
          timePickerDialog.show();
  
      }
  
      TimePickerDialog.OnTimeSetListener onTimeSetListener
              = new TimePickerDialog.OnTimeSetListener() {
  
          @Override
          public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  
              Calendar calNow = Calendar.getInstance();
              Calendar calSet = (Calendar) calNow.clone();
  
              calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
              calSet.set(Calendar.MINUTE, minute);
              calSet.set(Calendar.SECOND, 0);
              calSet.set(Calendar.MILLISECOND, 0);
  
              if (calSet.compareTo(calNow) &lt;= 0) {
                  //Today Set time passed, count to tomorrow
                  calSet.add(Calendar.DATE, 1);
              }
  
              setAlarm(calSet,RQS_1);
          }
      };
  
      private void setAlarm(Calendar targetCal,int RQS) {
  
          if(RQS==1){
              textAlarmPrompt.setText(
                      "\n\n***\n"
                              + "Alarm is set@ " + targetCal.getTime() + "\n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
          }else if(RQS==2){
              textAlarmPromptOne.setText(
                      "\n\n***\n"
                              + "Alarm is set@ " + targetCal.getTime() + "\n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
          }else if(RQS==3){
              textAlarmPromptTwo.setText(
                      "\n\n***\n"
                              + "Alarm is set@ " + targetCal.getTime() + "\n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
          }else if(RQS==4){
              textAlarmPromptThree.setText(
                      "\n\n***\n"
                              + "Alarm is set@ " + targetCal.getTime() + "\n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
          }
  
      }
  
      private void cancelAlarm(int RSQ) {
  
          if(RSQ==1){
              textAlarmPrompt.setText(
                      "\n\n***\n"
                              + "Alarm Cancelled! \n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RSQ, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.cancel(pendingIntent);
          }else if(RSQ==2){
              textAlarmPromptOne.setText(
                      "\n\n***\n"
                              + "Alarm Cancelled! \n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RSQ, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.cancel(pendingIntent);
          }else if(RSQ==3){
              textAlarmPromptTwo.setText(
                      "\n\n***\n"
                              + "Alarm Cancelled! \n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RSQ, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.cancel(pendingIntent);
          }else if(RSQ==4){
              textAlarmPromptThree.setText(
                      "\n\n***\n"
                              + "Alarm Cancelled! \n"
                              + "***\n");
  
              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RSQ, intent, 0);
              AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              alarmManager.cancel(pendingIntent);
          }
  
      }
  }
  </code></pre>
  
  <p><strong>AlarmReceiver.java</strong></p>
  
  <pre><code>import android.app.NotificationManager;
  import android.app.PendingIntent;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.media.RingtoneManager;
  import android.net.Uri;
  import android.widget.Toast;
  import android.support.v4.app.NotificationCompat;
  
  /**
   * Created by PC2 on 1/18/2017.
   */
  
  public class AlarmReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          Toast.makeText(context, "I'm running", Toast.LENGTH_LONG).show();
  
          NotificationManager notificationManager = (NotificationManager) context
                  .getSystemService(Context.NOTIFICATION_SERVICE);
  
          Intent notificationIntent = new Intent(context, MainActivity.class);
          notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                  notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
          Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
  
          NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                  context).setSmallIcon(R.mipmap.ic_launcher)
                  .setContentTitle("New Alarm")
                  .setContentText("Countdown").setSound(alarmSound)
                  .setAutoCancel(true)
                  .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
          notificationManager.notify(0, mNotifyBuilder.build());
      }
  }
  </code></pre>
  
  <p>Have any about my problem ?</p>