1

I used this code to create an alarm and it works. Please suggest how to cancel that alarm.

Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
            alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, mEdtTitle.getText()
                    .toString());
            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.HOUR, mTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, mTimePicker.getCurrentMinute());

            calendar.set(Calendar.DATE, mDatePicker.getDayOfMonth());
            calendar.set(Calendar.MONTH, mDatePicker.getMonth());
            calendar.set(Calendar.YEAR, mDatePicker.getYear());

            calendar.add(Calendar.MINUTE, 1);
            alarmIntent.putExtra(AlarmClock.EXTRA_HOUR,
                    calendar.get(Calendar.HOUR_OF_DAY));
            alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES,
                    calendar.get(Calendar.MINUTE));
            alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
startActivity(alarmIntent);

Thanks in advance

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
PrvN
  • 2,385
  • 6
  • 28
  • 30

2 Answers2

1

You need to use the method cancel(...) from AlarmManager, using the same PendingIntent you used to set the alarm. Example:

this.getAlarmManager().cancel(mAlarmPendingIntent); (this refers to the Activity or the Service from which you are cancelling the alarm).

Create the PendingIntent as:

mAlarmPendingIntent = PendingIntent.getActivity(this, requestCode, intent, flags);

CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
  • can you please help me to solve this https://stackoverflow.com/questions/51405397/cannot-cancel-the-current-alarm-in-android @CodeWarrior – Zhu Jul 23 '18 at 15:42
  • Canceling the PendingIntent does not cancel the alarm already set in the alarm clock. It only cancels the PendingIntent which is the ability to set the alarm in the first place. Once it's set, it cannot be unset by canceling the PendingIntent. – fsljfke Aug 16 '20 at 18:29
1

You have to invoke the cancel method: link

The PendingIntent should be same that you have set before with AlarmManager.

Naddy
  • 2,664
  • 6
  • 25
  • 38