Search code examples
androidandroid-intentalarmandroid-pendingintent

Pending Intent only work for 1 alarm


Currently, I have an alarm that works using pendingIntennt that works when a button(save an alarm) is pressed. The problem is, when I have multiple alarm scheduled and saved, the alarm that will go off is only the latest one. How can I make it so all the alarms will go off?

This is the code inside of the button:

public void saveReminder(View v) {

    final int reminderHour = mReminderTimePicker.getCurrentHour().intValue();
    final int reminderMinute = mReminderTimePicker.getCurrentMinute().intValue();
    final String reminderName = mReminderName.getText().toString();
    final boolean reminderStatus = mReminderSwitchSwitch.isChecked();

    //AlarmManagerHelper.cancelAlarms(this);
    //Updating Reminder Information
    ParseQuery<ParseObject> query = ParseQuery.getQuery("MyReminders");
    query.getInBackground(objectId, new GetCallback<ParseObject>() {
        public void done(ParseObject reminderObject, ParseException e) {
            if (e == null) {
                reminderObject.put("reminderName", reminderName);
                reminderObject.put("reminderHour", reminderHour);
                reminderObject.put("reminderMinute", reminderMinute);
                reminderObject.put("reminderStatus", reminderStatus);
                reminderObject.saveInBackground();
            }
        }
    });
    //set time into calendar instance
    GregorianCalendar calendar= new GregorianCalendar();
    calendar.set(GregorianCalendar.HOUR_OF_DAY,reminderHour);
    calendar.set(GregorianCalendar.MINUTE,reminderMinute);
    calendar.set(GregorianCalendar.SECOND,00);
    calendar.set(GregorianCalendar.MILLISECOND,0000);

    AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


    Intent reminderintent = new Intent(this, ReminderService.class);
    //push information to reminder screen
    reminderintent.putExtra("reminderName", reminderName);
    reminderintent.putExtra("reminderHour", reminderHour);
    reminderintent.putExtra("reminderMinute", reminderMinute);


    PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 1, reminderintent, PendingIntent.FLAG_UPDATE_CURRENT);
    reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation);


    finish();
}

Solution

  • It looks like you're using the same requestCode of 1 for all your alarms, and so each time you set an alarm, it overwrites the previous one when you call PendingIntent.getActivity().

    What you need is a way to have a unique ID for each alarm.

    Using the objectId String, you could generate a unique ID by adding up all of the characters in the String:

        int id = 0;
        for (int i = 0; i < objectId.length(); i++){
            id += objectId.charAt(i);
        }
    
        PendingIntent operation = PendingIntent.getActivity(getBaseContext(), id, reminderintent, PendingIntent.FLAG_UPDATE_CURRENT);
        reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation);