Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pending intent with different intent but same ID

I have two pending Intent to use with Alarm Manager one is:

Intent i = new Intent(context, TriggerAlarm.class);  
PendingIntent pi =PendingIntent.getBroadcast(context,0,i,PendingIntent.FLAG_CANCEL_CURRENT);

and the other is:

 Intent i = new Intent(context, TriggerNotification.class);
 PendingIntent pi = PendingIntent.getBroadcast(context,0, i,PendingIntent.FLAG_CANCEL_CURRENT);

I use these two in different methods in my application

my question is:

Are these pendingIntents differnt from each other?? because the intents are different but the Ids are same

If I set alarm manager for each of these pending intent do both of them trigger or one replace another?

like image 261
mj7000 Avatar asked Aug 04 '16 04:08

mj7000


2 Answers

So the easy way is test it directly by yourself. I have tested it on my computer and here is what i got:

Are these pendingIntents different from each other?? because the intents are different but the Ids are same

-Yes they are different each other although the Ids are same

If I set alarm manager for each of these pending intent do both of them trigger or one replace another?

-Both of them will be triggered

Here are my code for test, you can copy and try it by yourself

Copy this method to your activity, and call it

private void setAlarmManager() {
    Log.v("AlarmManager", "Configuring AlarmManager...");
    Intent startIntent1 = new Intent(context, AlarmReceiverFirst.class);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, startIntent1, PendingIntent.FLAG_CANCEL_CURRENT);

    Intent startIntent2 = new Intent(context, AlarmReceiverSecond.class);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 20);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Log.v("AlarmManager", "Starting AlarmManager for >= KITKAT version");
        alarm.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);
        alarm.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent2);
    } else {
        Log.v("AlarmManager", "Starting AlarmManager for < KITKAT version");
        alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);
        alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent2);
    }

    Log.v("AlarmManager", "AlarmManager has been started");
}

Create your first receiver class

public class AlarmReceiverFirst extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(this.getClass().getSimpleName(), "first alarm receiver is called");
    }
}

Create your second receiver class

public class AlarmReceiverSecond extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(this.getClass().getSimpleName(), "second alarm receiver is called");
    }
}

Register those receivers to your manifest

<receiver android:name=".AlarmReceiverFirst" />
<receiver android:name=".AlarmReceiverSecond" />

Not to be confused, what you called Id here is called request code. It is used for cancelling the pending intent.

like image 148
HendraWD Avatar answered Sep 21 '22 03:09

HendraWD


Intents are just the action PendingIntent is bound to execute once it triggers. But this triggering criteria are entirely depending on PendingIntent itself and RequestCode is playing here a pretty good role to uniquely identify, manage and trigger PendingIntent.

Therefore, no matter what the Intent is, if the requestCode is repeated then the latter PendingIntent will trigger. If you need to trigger multiple PendingIntents, the requestCode must be different from each other.

like image 45
fluffyBatman Avatar answered Sep 20 '22 03:09

fluffyBatman