有人可以为设定每周重复的几天的警报提供良好的逻辑吗?我已经通过使用每周警报

            alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt);
            alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt);
            alarmCalendar.set(Calendar.SECOND, 0);
            alarmCalendar.set(Calendar.AM_PM, amorpm);

            Long alarmTime = alarmCalendar.getTimeInMillis();

Intent intent = new Intent(Alarm.this, AlarmReciever.class);
                intent.putExtra("keyValue", key);
                PendingIntent pi = PendingIntent.getBroadcast(Alarm.this, key, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7*1440*60000 , pi);

警报按时触发,并在7天后自动触发。

但是我的要求是我要选择天数,而不是仅仅选择7天。

类似于每个星期一,星期二,星期四的9:00 AM-警报应自动触发。我该如何在setRepeating中执行此操作。

有人可以帮我这个忙吗?

谢谢!

最佳答案

这些问题谈论的是您想要的同一件事。这些答案将有所帮助:

您只需要指定开始的日期,然后每7天重复一次即可。在给定问题的答案中指定了几种方法:

How can i get the repeat alarm for week days using alarm manager?

Android Notification on specific weekday goes off directly

how to repeat alarm week day on in android

更新:

在您的评论中,您说



我的理解是,如果今天是星期二,如何为星期三重复设置警报,对吗?首先,是的,您可以使用多重ID分别设置每天的警报。

然后,您可以将alarmCalendar.set(Calendar.DAY_OF_WEEK, week);行添加到现有代码中。根据工作日(从1到7),该日重复一次。您可以将其作为参数传递给函数。喜欢:

    setAlarm(2); //set the alarm for this day of the week

    public void setAlarm(int dayOfWeek) {
        // Add this day of the week line to your existing code
        alarmCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

        alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt);
        alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt);
        alarmCalendar.set(Calendar.SECOND, 0);
        alarmCalendar.set(Calendar.AM_PM, amorpm);

        Long alarmTime = alarmCalendar.getTimeInMillis();
        //Also change the time to 24 hours.
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 , pi);
}

我从上述问题之一中提取了示例。希望它现在更加清晰。

09-27 12:56
查看更多