How to set multiple alarms using Android Alarm Manager

I am creating an emergency application. I have successfully implemented the 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, the user can select the days (Sunday, Monday ...) to repeat the alarm weekly. I am trying to create some alarms to repeat weekly, but don’t know how to do it. can I create it using (repeat) the interval or create multiple alarm dispatchers?

+45
android alarmmanager
Dec 12 2018-11-12T00:
source share
4 answers

You need to use different Broadcast id's for 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 system time should be a unique identifier for each waiting expectation, making you shoot.

+120
Apr 10 2018-12-12T00:
source share

From documents :

If there is already an alarm for this planned intention ( filterEquals (Intent) is defined with the equality of two intentions, then it will be deleted and replaced with this

Multiple AlarmManagers will not solve your problem. If they have several different alarms (different times and different days), then you will need to set an alarm within the BroadcastReceiver every time you start a previous alarm.

You will also need to hold RECEIVE_BOOT_COMPLETED and have BroadcastReceiver to receive the download, so that if the phone is rebooted, you can reschedule your alarms.

+10
Dec 12 2018-11-12T00:
source share

To set up several alarms, you need to define your Intent each time so that it is different from the others. The easiest way to find this: set the data field of your Intent 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 @ Hassy31 code is beautiful as it is and remains unchanged.

Please note that the PendingIntent.getBroadcast() parameter in the PendingIntent.getBroadcast() method (as suggested by @parag) is not used according to the documentation, so this is not the right way to do this.

+7
Nov 21 '12 at 2:52
source share

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); } 
+2
Sep 11 '15 at 3:07 on
source share



All Articles