问题
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
回答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);
回答2:
You have to invoke the cancel method: link
The PendingIntent
should be same that you have set before with AlarmManager
.
来源:https://stackoverflow.com/questions/20159649/how-to-cancel-alarm-programmatically-in-android