Search code examples
androidalarmmanageralarm

Android how to not cancel previous alarm while setting new one


Ok, as it is in AlarmManager.set:

If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled.

How can I change this default behaviour?
I need to shedule alarms only once.
And if new alarms will be sheduled (with the same PengingIntent), I need to ignore them.

So I need the new alarm to be cancelled, but previous to be valid.


Solution

  • Ok, got it.

    I created PendingIntent with no FLAG option.
    Then I have method:

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

    When I'm tryin' to set new Alarm - first I check - is this PendingIntent present.
    And, when Alarm fires - I cancel myPendingIntent manually by calling cancel().

    This works good, as I wanted.