Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android alarm events gets delayed

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

like image 376
user93796 Avatar asked Dec 16 '15 07:12

user93796


1 Answers

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.

like image 129
Konstantin Loginov Avatar answered Oct 05 '22 12:10

Konstantin Loginov