Search code examples
androidnotificationsalarmmanageralarmandroid-alarms

Is it possible to put the multiple date alarm with the this code in android?


In my Application i want to set the Different alarm on defferent date. So is it possible with this code:

//For Broadcast Alarm
                alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
                Intent in = new Intent(this, AlarmReceiverNotificationForTwoMonth.class);
                pendingIntentOfTwoMonth = PendingIntent.getBroadcast(this, 0, in, 0);

                // for the GST 20 June 2011
                Calendar calendar_GST_18_June_2011 = Calendar.getInstance();
                calendar_GST_18_June_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_18_June_2011.set(2011, 5, 18, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,   calendar_GST_18_June_2011.getTimeInMillis(), pendingIntentOfTwoMonth); 

                // for the GST 17 August 2011
                Calendar calendar_GST_17_August_2011 = Calendar.getInstance();
                calendar_GST_17_August_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_17_August_2011.set(2011, 7, 17, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_17_August_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 19 October 2011
                Calendar calendar_GST_19_October_2011 = Calendar.getInstance();
                calendar_GST_19_October_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_19_October_2011.set(2011, 9, 19, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_19_October_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 17 December 2011
                Calendar calendar_GST_17_December_2011 = Calendar.getInstance();
                calendar_GST_17_December_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_17_December_2011.set(2011, 11, 17, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_17_December_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 18 February 2012
                Calendar calendar_GST_18_February_2012 = Calendar.getInstance();
                calendar_GST_18_February_2012.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_18_February_2012.set(2012, 1, 18, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_18_February_2012.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 27 April 2012
                Calendar calendar_GST_27_April_2012 = Calendar.getInstance();
                calendar_GST_27_April_2012.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_27_April_2012.set(2011, 3, 27, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_27_April_2012.getTimeInMillis(), pendingIntentOfTwoMonth);

Thanks.


Solution

  • No it is not. You will only have 1 alarm, the last one you created. The reason for this is because the PendingIntent is the same. You need to use unique PendingIntents in order to get multiple alarms. I suggest reading the API docs on AlarmManager and Notifications.