3 回答

TA貢獻2021條經(jīng)驗 獲得超8個贊
如果要設(shè)置多個警報(重復(fù)警報或單個警報),則只需創(chuàng)建PendingIntent帶有不同的警報requestCode。如果requestCode相同,則新警報將覆蓋舊警報。
這是創(chuàng)建多個單個警報并將其保留的代碼ArrayList。我將保留PendingIntent在數(shù)組中,因為這是取消警報所需的。
// 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條經(jīng)驗 獲得超8個贊
您可以設(shè)置警報的重復(fù)次數(shù):
在這種情況下:
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 關(guān)注
- 495 瀏覽
添加回答
舉報