Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple alarms using alarm manager in android

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 (repeat) interval or should I create multiple alarm managers?

like image 850
Hassy31 Avatar asked Dec 12 '11 03:12

Hassy31


People also ask

How do I set multiple alarms on Android?

To access the alarms, tap the alarm icon at the top of the screen. To add a new alarm, tap the plus icon button at the bottom of the screen. This button can be used to add multiple alarms. To set the time for the alarm, tap the hour on the time on the left and then tap the hour on the clock on the right.

Can I set multiple alarms?

Although we're all different, generally speaking, setting several alarms is not recommended. Our brain is healing and “re-setting” during deep sleep. Angela Bradley, the principal psychologist at the Mood and Mind Centre, describes “multiple alarm episodes” as causing unnecessary disruption to these natural processes.

How do I use alarm Manager?

This example demonstrates how do I use AlarmManager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

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.

like image 182
Parag Chauhan Avatar answered Sep 18 '22 04:09

Parag Chauhan


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.

like image 38
Reed Avatar answered Sep 18 '22 04:09

Reed