在Android的一周报警设置重复天Android

2023-09-05 07:28:59 作者:往事并不人烟

有人可以提供良好的逻辑的一周报警设定重复的日子吗?我已经通过每周进行报警

Can somebody give good logic for set repeat days of week alarm? I have done weekly Alarm by using

            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天,它会自动触发在那个时候。

The alarm trigger on time and after 7 days it automatically triggers at that time.

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

But my requirement is I want to choose days rather than just 7 days.

像每周一,周二,周四上午9点 - 报警应自动触发。我该如何去有关setRepeating这样做。

something like every Monday, Tuesday, Thursday at 9:00 AM - Alarm should trigger automatically. How do I go about doing this in setRepeating.

有人可以帮我这个?

谢谢!

推荐答案

这些问题谈论同样的事情,只要你想。这些问题的答案将是有益的:

These questions talk about the same thing as you want. Those answers will be helpful:

您只需要到指定的一天的开始,然后重复它每隔7天。有上给出的问题答案中指定的几个方面:

You just need to specify the day to start it and then repeat it every 7 days. There are few ways specified in answers on given questions:

How我可以使用报警经理周天重复报警?

Android在具体工作日通知熄灭直接

如何重复报警本周日在机器人

更新:

在你的评论说

如何设置triggerAtMillis部分setRepeating。比方说今天是星期二,我选择每周的周一,周三,周五。 - 做什么,我把周三

How to set the triggerAtMillis part in setRepeating. say for example today is Tuesday, I choose weekly Monday, Wednesday, Friday. - What do I put for Wednesday ?

我的理解是,如果今天是星期二,如何设置报警让说周三的重复,对不对?首先是你可以使用mulltiple的ID分别设置报警每一天。

What I understood that that if today is Tuesday, how to set alarm for lets say Wednesday repeating, right? First of all yes you can use mulltiple id's to set alarms for each day separately.

然后你可以添加 alarmCalendar.set(Calendar.DAY_OF_WEEK,周); 行现有的code。根据本周日(1-7),它重复的那一天。你可以将它传递给函数的参数。像:

Then you can add alarmCalendar.set(Calendar.DAY_OF_WEEK, week); line to your existing code. Based on the week day( from 1-7) it repeats for that day. You can pass it into a function as parameter. Like:

    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); 
}

我已经采取的例子从上面的问题之一。希望它更清楚了吧。

I've taken the example from one of above question. Hope its more clear now.