Does service.startForeground imply wakelock?
Asked Answered
A

2

6

Question

I am wondering if we need to aquire the WakeLock or if the service.startForeground() command does that anyway for us? I didn't find anything in the documentation.

Goal

We want to remove unnesessary stuff which might slow down the service start as we want to start capturing the sensor data as soon as possible and as the service might be restarted frequently.

Context

We're developing an Android library to capture sensor data with ~ 200 Hz for up to a couple of hours (research environment). Right now we aquire a WakeLock and start the capturing service as ForegroundService to make sure our capturing isn't stopped when the device is not used.

Apparatus answered 24/8, 2018 at 10:29 Comment(0)
A
14

To answer my own question (if no one else finds more details on this):

In my understanding "foreground" just describes that the user would notice if the app was killed e.g. when browsing a large page. For this reason the system is asked to try to avoid killing the service.

  • Just because a foreground service is running probably not implies that the CPU can't go into deep sleep mode.

In order to collect sensor data all the time from user-defined start to stop we still need to aquire the WakeLock ourselves.

Apparatus answered 24/8, 2018 at 10:52 Comment(3)
So if we dont acquire a wake lock to a front service, then the service will pause in sleep mode? If yes, then afterwards will the service continue from where it left off or restart?Coventry
From my understanding: yes, the service can go into sleep mode and it will continue (not restart) afterwards.Apparatus
what if the foreground service register broadcast receiver to get events about power connection (charging) or if it uses camera in background? Do I still need the wake lock?Transoceanic
G
2

No, wakelock can be used to make sure your foreground services run when device goes to sleep

If you must use partial wake locks, follow these recommendations:

  • Make sure some portion of your app remains in the foreground. For example, if you need to run a service, start a foreground service instead. This visually indicates to the user that your app is still running.

https://developer.android.com/topic/performance/vitals/wakelock#best_practices

Gosh answered 7/3, 2019 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.