Doze Mode and Foreground Service

Doze mode - do foreground services continue to run?

Foreground services are not killed in doze mode and it's a great workaround to override the doze mode.
Killing the foreground service is highly depending on Mobile OS.
Like Huawei, it kills the foreground services after a slack of time and you will not be able to determine the period.
Some other phones kill the oldest foreground service if it detects unexpected battery consumption.
last year, I spent around 6 months observing phones behavior in killing the foreground services when doze mode is activated or not.
and I tried more than one solution to override the doze mode in detecting location every 10 seconds and the best one is the foreground service.
So you will face unexpected behavior on some phones but it is the best solution for doze and standby mode.
You can see this article
and you can also look at this tutorial

Keep ForegroundService alive When in Doze Mode

As per this, the following is a restriction which applies in doze mode :

The system ignores wake locks.

You still need to hold a wakelock "to indicate that your application needs to have the device stay on".

So, your wake lock will not affect doze mode (and that is by design). However, if you are using a foreground service, doze mode does not seem to affect foreground services see this SO post.

If you are using something else, you can see this guide here to optimize your app for doze mode.

Doze mode and foreground service

In this post's comments on Sep 17 Dianne Hackborn writes:

Apps that have been running foreground services (with the associated notification) are not restricted by doze.

So yes, your findings seem to be the intended behavior.

Android Kotlin : Getting location when app is in doze mode

You cannot receive locations when the device is in doze mode. Quoting a comment by a Google representative on an issue on the Google issue tracker:

location not delivered in doze mode is by design. When doze mode is active we do best effort location delivery, i.e, we deliver location if the phone wakes up anyways for some other reason, but otherwise we will not wake up the phone just to deliver location

Reference

Note:

You may say that the Foreground Service should not be affected by doze mode. It is not. It is the location provider that is.

Update:

Doze mode is only activated "if a user leaves a device unplugged and stationary for a period of time, with the screen off". Thus if a device is stationary in one location there is no need to track location for that time period.

Don't know if Doze mode is your issue. Battery optimization is another issue. For certain phones, the location providers are disabled when battery optimization is turned on and the screen is turned off. You can check this behavior using PowerManager.getLocationPowerSaveMode(). The modes are:

  1. LOCATION_MODE_FOREGROUND_ONLY
  2. LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF
  3. LOCATION_MODE_NO_CHANGE
  4. LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF

Also, certain phone vendors have created phones that do not comply with standard Android behaviour. Check this.



Related Topics



Leave a reply



Submit