所以我有一个这样的应用程序,您可以在其中创建多个日记,并且我想为每个日记设置提醒。例如,在其中一本日记中,我设置了提醒,使其在每周的星期二和星期五重复一次,然后在另一本日记中,我想在同一天设置不同的提醒,我该如何做,以使提醒不会互相取消?因为无法将一个警报管理器设置为在不同日期重复,所以我为每个选定的日期设置了多个具有不同requestCode的警报管理器,但是如果我为其他具有相同requestCodes的日记警报管理器设置,它们将取消以前的提醒。我该如何解决? (用户可以添加多个日记,因此不能选择不同的广播接收方)
fun setJournalReminder(context: Context, cls: Class<*>, hour: Int, minute: Int, day: Int){
val calendar = Calendar.getInstance()
val setCalendar = Calendar.getInstance().apply {
timeInMillis = System.currentTimeMillis()
set(Calendar.HOUR_OF_DAY, hour)
set(Calendar.MINUTE, minute)
set(Calendar.SECOND, 0)
}
when (day) {
1 -> setCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
2 -> setCalendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY)
3 -> setCalendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY)
4 -> setCalendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY)
5 -> setCalendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY)
6 -> setCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY)
7 -> setCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
}
if (setCalendar.before(calendar))
setCalendar.add(Calendar.DATE, 1)
val receiver = ComponentName(context, cls)
val pm = context.packageManager
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
val intent = Intent(context, cls)
val pendingIntent = PendingIntent.getBroadcast(context, day, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setCalendar.timeInMillis, AlarmManager.INTERVAL_DAY * 7, pendingIntent)
}
答案 0 :(得分:1)
您将当天用作requestCode,因此在同一天,待处理的意图将被更新。我认为您应该结合使用day和journalId来创建您的RequestCode(请求代码对于Journal和Day必须是唯一的)
我们可以将请求代码添加到Intent操作中,以使Intent.filterEquals始终为false ...