0

I want to be able to register two alarms to the same BroadcastReceiver. However, the first alarm never gets fired. How can I make this work?

Calendar now = Calendar.getInstance();
    now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 5);
    long trigger1 = now.getTimeInMillis();
    now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 10);
    long trigger2 = now.getTimeInMillis();

    Intent startIntent = new Intent(AlarmStartReceiver.START_ALARM);
    startIntent.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 4);
    PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent startIntent2 = new Intent(AlarmStartReceiver.START_ALARM);
    startIntent2.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 5);
    PendingIntent startPIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, PendingIntent.FLAG_UPDATE_CURRENT);


    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, trigger1, startPIntent);
    alarm.set(AlarmManager.RTC_WAKEUP, trigger2, startPIntent2);

Only the second one goes off. How can I make them both go off?

EDIT FOR ANSWER: Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method android pending intent notification problem

Community
  • 1
  • 1
user123321
  • 12,025
  • 11
  • 49
  • 63

2 Answers2

1

Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method android pending intent notification problem

user123321
  • 12,025
  • 11
  • 49
  • 63
0

Are you looking for separate notification event for each alarm you are setting? Or it has to be same notification with number of alarm events showing on the status bar icon?

Look at this post on how you can use "setData()" to the intent to create separate alarms.

Alarm Manager - Scheduling multiple Non-repeating events

Community
  • 1
  • 1
inder
  • 541
  • 1
  • 5
  • 9