On your pendingIntent you need to set the second requestCode to a unique number. I usually run the array through a for loop and dynamically set the request code for each element of the array. Without requestCode alarms overwrite 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 give it a new unique identifier for each element of the array. Keep in mind that if you want to cancel alarms, you will need to use another for the loop and cancel each separately. I personally add all my alarms to my own array so that 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(); } }
ryandlf Aug 31 '11 at 6:30 a.m. 2011-08-31 06:30
source share