Android设置多个警报

我正在尝试实现一个Android应用,该应用需要同时发出多次警报(或发出警报)。

我已经搜索过了,但是发现的最接近的是设置了固定数量的警报,并且我猜这个例子没有用。

我想知道是否存在一种方法来动态设置多个警报,例如警报数组,然后在其特定时间戳中触发这些警报。


小怪兽爱吃肉
浏览 419回答 3
3回答

宝慕林4294392

如果要设置多个警报(重复警报或单个警报),则只需创建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){&nbsp; &nbsp;Intent intent = new Intent(context, OnAlarmReceiver.class);&nbsp; &nbsp;// Loop counter `i` is used as a `requestCode`&nbsp; &nbsp;PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);&nbsp; &nbsp;// Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)&nbsp; &nbsp;mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemClock.elapsedRealtime() + 60000 * i,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pendingIntent);&nbsp;&nbsp; &nbsp;intentArray.add(pendingIntent);}

largeQ

您可以设置警报的重复次数:在这种情况下:public void AddAlarm(int requestCode,MutableDateTime dueDate,int repeat) {&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(context, AlarmReceiver.class);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(Constants.RECORD_ID, requestCode);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("REPEAT", repeat);&nbsp; &nbsp; &nbsp; &nbsp; PendingIntent operation = PendingIntent.getBroadcast(context, requestCode, intent,&nbsp; PendingIntent.FLAG_ONE_SHOT );&nbsp; &nbsp; &nbsp; &nbsp; MutableDateTime due = dueDate.toMutableDateTime();&nbsp; &nbsp; &nbsp; &nbsp; switch(repeat){&nbsp; &nbsp; &nbsp; &nbsp; case NO_REPEAT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; due.addMinutes(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case DAILY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; due.addDays(1);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case WEEKLY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; due.addWeeks(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MONTHLY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; due.addMonths(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MONTHLY_2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; due.addWeeks(5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case YEARLY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; due.addYears(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; due.add(-(dueDate.getMillis()));&nbsp; &nbsp; &nbsp; &nbsp; due.setSecondOfMinute(0);&nbsp; &nbsp; &nbsp; &nbsp; dueDate.setSecondOfMinute(0);&nbsp; &nbsp; &nbsp; &nbsp; alarm.cancel(operation);&nbsp; &nbsp; &nbsp; &nbsp; alarm.set(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), operation);&nbsp; &nbsp; &nbsp; &nbsp; alarm.setRepeating(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), due.getMillis(), operation);}
打开App,查看更多内容
随时随地看视频慕课网APP