PetoPeto
PetoPeto

Reputation: 21

Android Notification on exact time

Is there any alternative to setExact() method of AlarmManager? I want to show Notifications on exact time, but I do not want to lift my minSdkVersion to 19. Now I am using set() method of AlarmManager but alarms scheduled far in the future doesn't notify user at the exact time (but cca 10 minutes later). Now I am using minSdkVersion 16 and targetSdkVersion 21. I found on Android Developers a note that says:

Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future. With the new batching policy, delivery ordering guarantees are not as strong as they were previously. If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requested delivery times. If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is before API 19 will continue to get the previous alarm behavior: all of their scheduled alarms will be treated as exact.

But using setExact() or setWindow() is not a solution for me. Thanks for reply.

Upvotes: 1

Views: 1023

Answers (1)

Phantômaxx
Phantômaxx

Reputation: 38098

Check the current API Level.
If below 19, then use set(), otherwise use setExact().

To check your OS version, use something like this:

In your imports section:

import android.os.Build;

In your declarations:

protected static final int Build_Version = Build.VERSION.SDK_INT;

Then it's easy to check

if(Build_Version < 19)
{
    // Use set()...
}
else
{
    // Use setExact()...
}

Upvotes: 2

Related Questions