Doze mode handling
Asked Answered
E

1

5

I am working on an application which spawns some services in the background using AlarmManager. Timing is very crucial to our application and the functionality can't wait for the next maintenance window to occur. Asking a user to white-list the application is not an issue but doesn't fix the issue of suspending alarms. Also battery consumption is not that big of an issue as well.

First possible solution that came to my mind was to spawn an always running foreground service to handle the rescheduling of services instead of AlarmManager but in doing so will shift most of the base structure of our application and is not feasible for us.

Current fix that I just implemented is to send a high priority push notification and on receiving the message, take full wake lock and turn on the screen to break doze mode.

I wanted to know if there is an alternate way of breaking doze mode? Also is it possible without taking a wake lock? Can there be some possible repercussions of implementing the aforementioned solution?

P.S. I am using UrbanAirship for push notifications.

Entozoic answered 5/1, 2017 at 12:36 Comment(4)
Also is it possible without taking a wake lock? is what possible?Zwolle
Is it possible to break doze on receiving high-priority push notification without turning on the screen ? I was considering to not show notification in tray but without that, turning screen on would be even more weird.Entozoic
I tried to resolve the similar issue with FCM message. but It wakes my app very short time. Did you have a good solution?Fantastic
#44080358 I have similar question here, if you can suggest anything, it would be helpful. Thanks.Fantastic
Z
7

You can not "break"/stop/disable doze mode, but there are ways to temporarily lift your app's restrictions while the device is dozing.

  1. A high priority FCM message.

FCM high-priority messages let you reliably wake your app to access the network, even if the user’s device is in Doze or the app is in App Standby mode. In Doze or App Standby mode, the system delivers the message and gives the app temporary access to network services and partial wakelocks, then returns the device or app to the idle state.

High-priority FCM messages do not otherwise affect Doze mode, and they don’t affect the state of any other app. This means that your app can use them to communicate efficiently while minimizing battery impacts across the system and device.

  1. An andAllowWhileIdle alarm set with AlarmManager.

Doze is particularly likely to affect activities that AlarmManager alarms and timers manage, because alarms in Android 5.1 (API level 22) or lower do not fire when the system is in Doze.

To help with scheduling alarms, Android 6.0 (API level 23) introduces two new AlarmManager methods: setAndAllowWhileIdle() and setExactAndAllowWhileIdle(). With these methods, you can set alarms that will fire even if the device is in Doze.

Note that the minimum interval between two alarms in doze mode is 9 minutes.


For both cases, your app is restored to full functionality (meaning: the doze restrictions do not apply) for a short period of time, when that time expires, the OS will reinstate the doze restrictions.

Note that you do not need to turn on the screen to execute code during either of these 'wake up' periods.

I don't have a source at hand, but I believe the short period I name is ~10 seconds.

Source & additional reading

Zwolle answered 5/1, 2017 at 14:38 Comment(2)
Hi Tim, I have a similar question, if you can have a time, please look at it. It would be really helpful for me. #44080358Fantastic
Source from the setAndAllowWhileIdle methods docs: "When the alarm is dispatched, the app will also be added to the system's temporary whitelist for approximately 10 seconds to allow that application to acquire further wake locks in which to complete its work."Dictatorial

© 2022 - 2024 — McMap. All rights reserved.