Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm not waking phone

I want my app to wake up every so often. The code I'm using to set the alarm is:

AlarmManager alarmManager = (AlarmManager) getSystemService( Context.ALARM_SERVICE );
alarmManager.setExact( AlarmManager.RTC_WAKEUP, System.currentTimeMillis( ) + pollInterval, pendingIntent );

There is code in the alarm handler to reset the alarm with the above code.

The problem is that when the phone is in deep sleep (eg, in the early hours of the morning), the alarm doesn't ring.

Any suggestions?

like image 590
Kevin Gilbert Avatar asked Nov 14 '25 13:11

Kevin Gilbert


1 Answers

If you are using a phone with Android 6.0 or more, you have to deal with the Doze mechanism. So, when the phone is in sleep state the AlarmManager will not work immediatly. The documentation says:

Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.

and

If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().

So, just try using setExactAndAllowWhileIdle() for API > 22

Documentation here

like image 169
GVillani82 Avatar answered Nov 17 '25 09:11

GVillani82



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!