3 回答
TA贡献2021条经验 获得超8个赞
如果要设置多个警报(重复警报或单个警报),则只需创建PendingIntent带有不同的警报requestCode。如果requestCode相同,则新警报将覆盖旧警报。
这是创建多个单个警报并将其保留的代码ArrayList。我将保留PendingIntent在数组中,因为这是取消警报所需的。
// context variable contains your `Context`
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
for(i = 0; i < 10; ++i)
{
Intent intent = new Intent(context, OnAlarmReceiver.class);
// Loop counter `i` is used as a `requestCode`
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
// Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000 * i,
pendingIntent);
intentArray.add(pendingIntent);
}
TA贡献2039条经验 获得超7个赞
您可以设置警报的重复次数:
在这种情况下:
public void AddAlarm(int requestCode,MutableDateTime dueDate,int repeat) {
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(Constants.RECORD_ID, requestCode);
intent.putExtra("REPEAT", repeat);
PendingIntent operation = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_ONE_SHOT );
MutableDateTime due = dueDate.toMutableDateTime();
switch(repeat){
case NO_REPEAT:
due.addMinutes(0);
break;
case DAILY:
due.addDays(1);
break;
case WEEKLY:
due.addWeeks(1);
break;
case MONTHLY:
due.addMonths(1);
break;
case MONTHLY_2:
due.addWeeks(5);
break;
case YEARLY:
due.addYears(1);
break;
}
due.add(-(dueDate.getMillis()));
due.setSecondOfMinute(0);
dueDate.setSecondOfMinute(0);
alarm.cancel(operation);
alarm.set(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), operation);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), due.getMillis(), operation);
}
- 3 回答
- 0 关注
- 467 浏览
添加回答
举报