How to cancel alarm programmatically in android? [duplicate]

此生再无相见时 提交于 2020-08-20 07:17:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!