Goltsev Eugene
Goltsev Eugene

Reputation: 3627

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.

Upvotes: 0

Views: 448

Answers (2)

Goltsev Eugene
Goltsev Eugene

Reputation: 3627

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.

Upvotes: 1

Lionel Briand
Lionel Briand

Reputation: 1813

I don't know a proper way to do this.

A workaround would be to know if pending intent was already schedule by AlarmManager before sending.

Store a timestamp describing date of intent with SharedPreference. Check if the timestamp was passed before sending another PendingIntent to AlarmManager.

Upvotes: 1

Related Questions