13

I would like to make a delay(10 min) for user then after it, user can edit something.

to do this,I created a setAlarm function :

public void setAlarm(Context context,int user,int time) {
   AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(context, sef_time.class);
   intent.putExtra(ONE_TIME, Boolean.FALSE);
   PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
   am.set(AlarmManager.RTC, 1000*60*time , pi); 
}

everything works fine, but my alarm manager has a delay. for example:

setAlarm(.....,int 10);

It has a delay : 00:10:03 second or 00:10:10 second 00:10:20 second !

where is my wrong ?

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

2 Answers2

23

As you can see here:

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.

If it's very important that the alarm be exact, use setExact (When the device's SDK is 19 or above).

Assaf Gamliel
  • 11,935
  • 5
  • 41
  • 56
  • First of all, thanks! it's working as expected. I don't get few things about android (as being a intermediate level developer) When I or any developer wants to set alarm "OBVIOUSLY" I want the alarm to be "EXACT" not like set alarm that will go off after 2 mins of original set time. There are few other things like these about android which confuses me time to time. – Alex Aug 24 '16 at 19:07
  • Hey @Alex, setting an alarm on Android causes it to "wake up" your process gets CPU time and battery power, this made the Android dev team decide to change the default implementation of `set` to be in "batches" meaning if something woke the device and the CPU is already doing something then "what the heck" let's do all other alarms that are close enough to now. I agree that this is not the best approach ("breaking" API) but I guess they felt that they had to do it to conserve battery (which a lot of users are complaining about). – Assaf Gamliel Aug 25 '16 at 08:11
  • Thank you! I couldn't figure it out the delay for the life of me, after upgrading to a new target API. Now I understand. – AdeelMufti Sep 07 '16 at 14:56
  • This is honestly the dumbest thing I have ever seen implemented in Android. They should have at least provided a flag which you could set to enable exact delivery timing or priority or something. What's the point of "saving battery' when things don't work as they are intended to be and are delayed? Completely stupid. – sudoExclaimationExclaimation Jan 08 '19 at 22:34
  • 1
    totally agree @PranoyC, I am working on VOIP incoming call. What's the point if there's delay. It looks broken. I receive notification that a incoming call is coming then I pass it to alarmmanager to broadcast and then show UI but alarmamanger takes 5 sec delay to broadcast it. – Alex Apr 18 '20 at 16:34
5

The easiest way to make system have a delay and then sound an alarm at the exact specified time is using setExact(), and the code can be something like this.

am.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (time_you_want_to_delay_in_milliseconds) ,pi);
  • I've checked it and in my case it works the same as set. I've scheduled alarm with 1s delay and it was delivered after 5s ... – LLL Mar 24 '18 at 10:55