how to set multiple alarms using android alarm man

2019-01-03 15:08发布

I'm building an alarm application.I have successfully implemented basic alarm functions.

Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, sHour);
calendar.set(calendar.MINUTE, sMin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
long sdl = calendar.getTimeInMillis();

Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmList.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager ALARM1 = (AlarmManager)getSystemService(ALARM_SERVICE);
ALARM1.set(AlarmManager.RTC_WAKEUP, sdl, sender);

in my application,user can select days(sunday,monday...) to repeat the alarm weekly. I'm trying to create multiple alarms to repeat weekly but don't know how to do it. can I create it using (repeate)interval or should I create multiple alarm managers?

4条回答
男人必须洒脱
2楼-- · 2019-01-03 15:14

From the docs:

If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent), then it will be removed and replaced by this one

Multiple AlarmManagers would not resolve your issue. If they have multiple different alarms (different times and different days), then you would need to set the alarm within the BroadcastReceiver every time you fire off a previous alarm.

You would also need to hold RECEIVE_BOOT_COMPLETED and have a BroadcastReceiver to receive the boot so that if the phone is rebooted you can re-schedule your alarms.

查看更多
狗以群分
3楼-- · 2019-01-03 15:19

You need to use different Broadcast id's for the pending intents. Something like this:

    Intent intent = new Intent(load.this, AlarmReceiver.class);
    final int _id = (int) System.currentTimeMillis();
    PendingIntent appIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT);

Using the system time should be a unique identifier for every pending intent you fire.

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-03 15:30

To set multiple alarms you need to define your Intent each time so that it is distinguishable from the others. The easiest way I find to do this is to set the data field of your Intent something as follows:

// give your alarm an id and save it somewhere
// in case you want to cancel it in future
String myAlarmId = ...;

// create your Intent
Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
intent.setData(Uri.parse("myalarms://" + myAlarmId));
...

The rest of your code @Hassy31 is fine as is and remains unchanged.

Note that the requestCode parameter in the PendingIntent.getBroadcast() method (as suggested by @parag) is unused according to the documentation so this ain't the right way to go about it.

查看更多
Lonely孤独者°
5楼-- · 2019-01-03 15:33

set Broadcast id for pendingIntent

for (int id = 0; id < 3; id++) {
        // Define pendingintent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id,ntent, 0); 
        // set() schedules an alarm 
        alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
}
查看更多
登录 后发表回答