Android alarm events gets delayed
Asked Answered
A

1

7

I have developed my android alarm app which need to something every hour(like 1pm, 2pm, 3pm ,4pm ,5pm ,6pm etc).

Now am using alarmManager so that I get a broadCast event every hour.But sometimes the event is delayed.

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Log.d(Constants.APP_TAG, "setting beep alarm");
PendingIntent  pendingIntent = PendingIntent.getBroadcast( context, 0, new Intent("com.mindedges.beephourly.intent.action.NEW_HOUR"),PendingIntent.FLAG_UPDATE_CURRENT );

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmSheduleHelper.getImmediateNextHour().getTimeInMillis(),AlarmManager.INTERVAL_HOUR, pendingIntent);

How can I make sure that I get broadcast event exactly on time.

PS: It gets delayed on some specific phones/anroid version

Ackler answered 16/12, 2015 at 7:35 Comment(0)
R
5

In Android 6.0 Marshmallow, Google introduced "Doze" mode.

Read more about it here: Optimizing for Doze and App Standby

And/or watch here: Android Marshmallow 6.0: Introduction to Doze Mode

In your case, Google recommends to use setAndAllowWhileIdle which force phone to bypass Doze mode instead set (and setExactAndAllowWhileIdle instead of setExact.)

NB! It looks like, there's no analogue for setRepeating, so you have to schedule next recurrence "manually" with setExactAndAllowWhileIdle, one by one.

For devices with Android 4.4 KitKat and above - consider to use setExact instead of old set, as the OS tries to shift alarms in order to minimize wakeups and battery use.

There might be more points to keep in mind - for example some OEMs might have their own utils preinstalled in order to save battery life, but at least it must cover the stock Android and presumably majority of popular devices.

Raffarty answered 16/12, 2015 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.