Search code examples
androidalarmmanageralarmandroid-pendingintentandroid-alarms

How to set Multiple AlarmManager on list Item?


I have the problem with my code. I have list with date and time. I get all date and time from SQLite in for loop. Now when i run program AlarmManager is not triggered on every date and time. Last added alarm only received. Here i have the code:

            private void setAlarm() {
            // TODO Auto-generated method stub
            DbHandler dbHandler =new DbHandler(getApplicationContext());
            list_alarm = dbHandler.getDateOnly();
            Log.d("00000", list_alarm.size()+"");
            if(list_alarm.size() > 0){
                for(int i=0;i<list_alarm.size();i++){
                    String dat = list_alarm.get(i).getTaskDate();
                    String pat = list_alarm.get(i).getTaskTime();
                    String strDnT = dat+" "+pat;
                    SimpleDateFormat format;
                    Date date1;
                    long d;
                    format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    try {
                        date1 = (Date)format.parse(strDnT);
                        d = date1.getTime();
                        Intent k = new Intent(Create_Task.this, AlarmReceiver.class);
                        PendingIntent intent = PendingIntent.getBroadcast(getApplicationContext(), RQS_1, k, 0);
                        AlarmManager managers = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                        managers.set(AlarmManager.RTC_WAKEUP, d, intent);
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }else{
                Log.d("List : ", "Not having any list");
            }

        }

Thanks in Advance. I hope You Understand my Question.


Solution

  • As Stated in android doc.

    If there is already an alarm scheduled for the same IntentSender, it will first be canceled.

    I believe you need to set the timing one by one: I mean when alarm is invoked with T1 then set for T2. when T2 is finished then set T3...