Dustfinger
Dustfinger

Reputation: 1023

Scheduling more than one pendingIntent to same activity using AlarmManager

Recently I noticed strange behaviour when i tried to schedule Activities to be run in the future using AlarmManager. Look at the code below, the first activity is started in 20 seconds, while the second activity is not started in 40 seconds instead it is started only after 60 seconds. Can anyone explain why the second intent doesn't schedule the second activity to be called instead the third intent does. Does this mean that i can have only one intent for an activity in the AlarmManager.

//pending intent for morning
    Intent myIntent1 = new Intent(this, Activity1.class);
    pendingIntent1 = PendingIntent.getActivity(this, 0, myIntent1, 0);
    AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar1 = Calendar.getInstance();
    //calendar1.set(Calendar.YEAR, Calendar.MONTH + 1, Calendar.DAY_OF_MONTH, morningTime, 0, 0);
    calendar1.setTimeInMillis(System.currentTimeMillis());
    calendar1.add(Calendar.SECOND, 20);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent1);

    //pending intent for noon
    Intent myIntent2 = new Intent(this, Activity2.class);
    pendingIntent2 = PendingIntent.getActivity(this, 0, myIntent2, 0);
    AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar2 = Calendar.getInstance();
    //calendar2.set(Calendar.YEAR, Calendar.MONTH + 1, Calendar.DAY_OF_MONTH, noonTime, 0, 0);
    calendar2.setTimeInMillis(System.currentTimeMillis());
    calendar2.add(Calendar.SECOND, 40);
    alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), pendingIntent2);

    //pending intent for night
    Intent myIntent = new Intent(this, Activity2.class);
    pendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    //calendar.set(Calendar.YEAR, Calendar.MONTH + 1, Calendar.DAY_OF_MONTH, nightTime, 0, 0);
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 60);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);'

Upvotes: 4

Views: 4374

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007659

Does this mean that i can have only one intent for an activity in the AlarmManager.

No, but it means that you need distinct PendingIntents. You are calling:

Intent myIntent = new Intent(this, Activity2.class);
pendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);

twice, and therefore the two getActivity() calls are returning the same PendingIntent.

Either:

  • Use a different value for the 2nd parameter to getActivity(), or

  • Do something to the Intent objects to make them sufficiently different, such as using different action strings (note: extras are not sufficient)

Upvotes: 15

Related Questions