简体   繁体   English

具有多个待定意图的Android警报管理器

[英]Android alarm manager with multiple pending intents

So I have such app where you can create multiple journals and I want to set reminders for each journal. 因此,我有一个这样的应用程序,您可以在其中创建多个日记,并且我想为每个日记设置提醒。 For example in one of the journals I set reminder to repeat every week on Tuesdays and Fridays and then in another journal I want to set different reminder on same days how can I do so that reminders wouldn't cancel each other? 例如,在其中一本日记中,我设置了提醒,使其在每周的星期二和星期五重复一次,然后在另一本日记中,我想在同一天设置不同的提醒,我该如何做,以使提醒不会互相取消? Because I cannot set one Alarm manager to repeat on different days I'm setting multiple alarm managers for each selected day with different requestCodes, but then if I set for other journal alarm managers with same requestCodes they cancel previous reminders. 因为无法将一个警报管理器设置为在不同日期重复,所以我为每个选定的日期设置了多个具有不同requestCode的警报管理器,但是如果我为其他具有相同requestCodes的日记警报管理器设置,它们将取消以前的提醒。 How could I solve this? 我该如何解决? (User can add multiple journals so different broadcast receivers are not an option) (用户可以添加多个日记,因此不能选择其他广播接收方)

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)

}

you are using the day as a requestCode, so for the same day the pending intent will be updated. 您将当天用作requestCode,因此在同一天,待处理的意图将被更新。 I think that you should use a combination of the day and a journalId to create your RequestCode(Request Code must be unique for a Journal and Day) 我认为您应该结合使用day和journalId来创建您的RequestCode(请求代码对于Journal和Day必须是唯一的)

We could add the request code to Intent action to make Intent.filterEquals always false... 我们可以将请求代码添加到Intent操作中,以使Intent.filterEquals始终为false ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM