我在写东西像一个提醒用户。用户将设置提醒他们的活动,在时机成熟时,重复报警将被设置为触发状态栏通知。但报警器似乎不停止后,我选择了通知或清除的通知。我不知道在哪里可以取消重复报警。下面是一些C $ CS的$的: 设置重复报警器在我的主要活动
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
与等效 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);