The*_*Man 13 java android alarmmanager repeatingalarm
有人可以为设定的重复周日警报提供良好的逻辑吗?我通过使用每周做一次警报
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);
Run Code Online (Sandbox Code Playgroud)
警报触发按时触发,7天后会自动触发.
但我的要求是我想选择几天而不是仅仅七天.
类似于每周一,周二,周四上午9:00 - 警报应自动触发.我如何在setRepeating中执行此操作.
有人可以帮我解决这个问题吗?
谢谢!
Sho*_*uri 15
这些问题谈论你想要的同样的事情.这些答案将有所帮助:
您只需指定开始日期,然后每7天重复一次.在给定问题的答案中指定的方法很少:
更新:
在你的评论中你说
如何在setRepeating中设置triggerAtMillis部分.比如说今天是星期二,我选择每周一,周三,周五. - 星期三我该怎么办?
据我所知,如果今天是星期二,如何设置警报让我们说星期三重复,对吧?首先是的,你可以使用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, alarmTime, 24 * 60 * 60 * 1000 , pi);
}
Run Code Online (Sandbox Code Playgroud)
我从上面的一个问题中得到了例子.希望现在更清楚.
小智 6
要为工作日设置重复闹钟,请使用以下代码。希望这是有帮助的。
Calendar calender= Calendar.getInstance();
calender.set(Calendar.DAY_OF_WEEK, weekNo); //here pass week number
calender.set(Calendar.HOUR_OF_DAY, hour); //pass hour which you have select
calender.set(Calendar.MINUTE, min); //pass min which you have select
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);
Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
if (calender.before(now)) { //this condition is used for future reminder that means your reminder not fire for past time
calender.add(Calendar.DATE, 7);
}
final int _id = (int) System.currentTimeMillis(); //this id is used to set multiple alarms
Intent intent = new Intent(activity, YourServiceClass.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, _id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27320 次 |
| 最近记录: |