Kevin Gilbert
Kevin Gilbert

Reputation: 725

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?

Upvotes: 4

Views: 486

Answers (1)

GVillani82
GVillani82

Reputation: 17439

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

Upvotes: 4

Related Questions