有人可以为设定重复几天的闹钟提供良好的逻辑吗?我每周使用报警
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这样的事件 – 报警应该自动触发.如何在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
更新:
在你的评论中你说
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来分别设置每天的闹钟.
然后可以添加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,24 * 60 * 60 * 1000,pi); }
我从上述问题中抽出了一个例子.希望现在更清楚.