Android Alarm Manager works but is delayed

I would like to make a delay (10 minutes) for the user, after which the user can edit something.

For this, I created the 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 is working fine, but my alarm manager has a delay. eg:

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

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

Where is my mistake?

+5
source share
2 answers

As you can see here :

Starting with API 19, the start time passed to this method is treated as inaccurate : the alarm will not be delivered until this time, but it can be delayed and delivered after some time. The OS will use this policy so that โ€œpacketโ€ alarms are displayed together throughout the system, minimizing the number of times the device should โ€œwake upโ€ and minimizing battery usage. In general, alarms planned in the near future will not be delayed until alarms planned far into the future.

With the new dosing policy, delivery guarantees are not as they were previously. If the application sets up multiple alarms, it is possible that the actual delivery order of these alarms may not match the order of their requested delivery dates. If your application has strong order requirements, there are other APIs that you can use to get the behavior you need; see setWindow (int, long, long, PendingIntent) and setExact (int, long, PendingIntent).

Applications whose targetSdkVersion before API 19 will continue to receive the previous alarm behavior: all their planned alarms will be considered accurate.

If itโ€™s very important that the alarm is accurate, use setExact (if the deviceโ€™s SDK is 19 or higher).

+16
source

The easiest way to make the system linger and then sound for a while is with setExact (), and the code might be something like this.

 am.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (time_you_want_to_delay_in_milliseconds) ,pi); 
+3
source

Source: https://habr.com/ru/post/1209987/


All Articles