Search code examples
androidalarmmanagerandroid-contextandroid-pendingintent

PendingIntent FLAG_NO_CREATE with different Context


Ok, I have next method:

private boolean isPresentPengingIntent() {
    return PendingIntent.getService(context,0,myIntent,PendingIntent.FLAG_NO_CREATE) != null;
}

myIntent is always the same.

I'm wondering - if I pass different context into this method, it returns false.

But if I plan new Alarm with:

PendingIntent.getService(context, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);

(also with different context) - it will erase previous alarm (because myIntent is the same, that's why PendingIntent is considering the same).

Why there is a different behaviour due to context?
Is there a way to ignore context parameter in my isPresentPengingIntent method?


Solution

  • The Context is not your problem. What you are seeing is a strangeness in the way PendingIntent.FLAG_ONE_SHOT works.

    You cannot determine if a PendingIntent exists or not using your code if you have created the PendingIntent with FLAG_ONE_SHOT. As far as Android is concerned, this PendingIntent does not exist.

    If you want to determine whether or not an alarm is set by using the existing of the PendingIntent as an indicator, then you cannot use FLAG_ONE_SHOT.