我正在编写类似于用户提醒的功能。用户将为他们的事件设置提醒,到时间后,将设置一个重复的闹铃来触发状态栏通知。但是,当我选择通知或清除通知后,闹铃似乎不会停止。我不确定在哪里取消这个重复的闹钟。以下是一些代码: 在我的主活动中设置重复闹铃。
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
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);
当用户选择通知或清除通知时,我该如何停止闹钟?
onCreate()
中放置取消闹钟的代码。 - CommonsWaregetBroadcast()
中使用那个flag呢? - laplasz