如何设置多个报警器在Android的时间?

如何设置多个报警器在Android的时间?

问题描述:

我想提出一个小的应用程序,我必须设置从阵列报警,但只有一个闹钟设置,并在同一时间是在阵列为什么它的行为就像这下最后一个位置的工作是我的code

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);

                    } 

在此先感谢

在你的 pendingIntent 您需要设置第二个请求code 来一个唯一的编号。我通常是通过运行阵列循环和动态设置请求code数组中的每个项目。如果没有请求code 的报警相互覆盖。

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);

}

基本上,你只是想请求code 更改为动态的数字。如果设置为 F 你给它一个新的唯一的ID为阵列中的每个项目。请记住,如果你想取消,你将需要使用另一个循环,并可单独取消每个报警。我个人所有的警报添加到各自为阵,所以我可以单独处理它们。

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();
    }
}