如何取消这个重复的闹钟? [英] How to cancel this repeating alarm?
问题描述
我正在写一些类似提醒用户的内容.用户会为他们的事件设置提醒,当时间到时,将设置重复警报以触发状态栏通知.但是在我选择通知或清除通知后,警报似乎没有停止.我不知道在哪里取消这个重复的闹钟.下面是一些代码:在我的主要活动中设置重复闹钟
I'm writing something like a reminder for users. Users will set reminders for their events, when the time comes, a repeating alarm will be set to trigger a status bar notification. But the alarm seems non-stop after I selected the notification or cleared the notification. I am not sure where to cancel this repeating alarm. Below are some of the codes: Set up the repeating alarm in my main activity
alarmTime = Calendar.getInstance();
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmTime.add(Calendar.MINUTE,offset_time);
//Schedule the alarm
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), 30 * 1000, sender);
在我的 OnReceive 方法中,我只是在状态栏中显示通知并将标志设置为 FLAG_AUTO_CANCEL
In my OnReceive method, I just display the notification in status bar and set the flag as FLAG_AUTO_CANCEL
manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.medical, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);
notification.flags = Notification.FLAG_AUTO_CANCEL;
manager.notify(R.string.service_text, notification);
如何在用户选择通知或清除通知时停止警报?
How can I stop the alarm when the user selects the notification or clears it?
推荐答案
在 AlarmManager
上调用 cancel()
并使用等效的 PendingIntent
到您与 setRepeating()
一起使用的一个:
Call cancel()
on AlarmManager
with an equivalent PendingIntent
to the one you used with setRepeating()
:
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
这篇关于如何取消这个重复的闹钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!