12

I am making a small app where I have to set alarm from array but only one alarm is set and working at a time which is at last position of array why it is behaving like this following is my code

AlarmManager[] alarmManager=new AlarmManager[24];
                for(f=0;f<arr2.length;f++)
                {
                    Intent intent = new Intent(AlarmR.this, Riciving.class);
                    pi=PendingIntent.getBroadcast(AlarmR.this, 0,intent, 0);

                    alarmManager[f] = (AlarmManager) getSystemService(ALARM_SERVICE);
                    alarmManager[f].set(AlarmManager.RTC_WAKEUP,arr2[f] ,pi);

                    } 

Thanks in Advance

Nitin
  • 1,948
  • 3
  • 22
  • 53
  • you can use this [More than one alarms](http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=3503) – Uttam Aug 31 '11 at 05:05
  • @Uttam Utils.getTimeAfterInSecs(45);is not working because there is no internal class Utils so how I can achieve the same – Nitin Aug 31 '11 at 06:12
  • Hello can you please post full code with receiver Multiple alarm at same time .. – Jayman Jani Jan 07 '17 at 10:41

1 Answers1

24

On your pendingIntent you need to set the second requestCode to a unique number. I usually run the array through a for loop and set the request code dynamically for each item in the array. Without the requestCode the alarms are overwriting each other.

AlarmManager[] alarmManager=new AlarmManager[24];
intentArray = new ArrayList<PendingIntent>();
for(f=0;f<arr2.length;f++){
   Intent intent = new Intent(AlarmR.this, Riciving.class);
   pi=PendingIntent.getBroadcast(AlarmR.this, f,intent, 0);

   alarmManager[f] = (AlarmManager) getSystemService(ALARM_SERVICE);
   alarmManager[f].set(AlarmManager.RTC_WAKEUP,arr2[f] ,pi);

   intentArray.add(pi);

}

Basically, you just want to change requestCode to a dynamic number. By setting it to f you are giving it a new unique id for every item in the array. Keep in mind, if you want to cancel the alarms you will need to use another for loop and cancel each one individually. I personally add all my alarms to their own array so I can handle them separately.

Then if you need to cancel them:

    private void cancelAlarms(){
    if(intentArray.size()>0){
        for(int i=0; i<intentArray.size(); i++){
            alarmmanager.cancel(intentArray.get(i));
        }
        intentArray.clear();
    }
}
Nikolai Samteladze
  • 7,337
  • 4
  • 41
  • 70
ryandlf
  • 22,189
  • 32
  • 97
  • 156
  • I updated my answer with some code. Keep in mind, this does not go along with the documentation. I had to deal with this not to long ago on a project I am working on. – ryandlf Aug 31 '11 at 13:06
  • 7
    Why the array of AlarmManager-s ? – KarolDepka Dec 27 '11 at 22:08
  • 1
    While relaunching the app, the array values are resettled. Unable to remove the alarm. using the static values also the values are resettled. Unable to achieve to cancel the alarms in this way of unique id generation. Could you please help me to cancel all the alarms? – Karthick Sep 26 '13 at 12:11
  • 1
    Hey Buddy.. You have saved my life .. Thanks :) – meen Feb 14 '16 at 08:34
  • I can't understand what array is `arr2`! Could someone help? – sam Nov 15 '18 at 12:44
  • What if Alarm no 1 was set then app was closed, then before alarm ringing, app is opened and another alarm i set. Wouldn't it overwrite the previous one, as the array index for the loop would be 0 for both alarms, so the request code would be same? – User9211 Mar 16 '21 at 12:57