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();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…