Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Foreground service vs. wakeLock

I am relatively new to Android, so what I am asking may seem obvious (although I have read all the similarly titled questions, and have searched extensively). I need to monitor the accelerometer continuously for long periods. Two approaches have been suggested:

1) acquire a partial wake lock that is held the entire time the acceleromtere is being monitored; and

2) monitor the accelerometer in a foreground service.

The first approach appears to use a lot of battery life. The second approach should result in a service that is only killed rarely, but I'm not sure what "rarely" means. Which approach should be used, and are there alternatives that I should consider?

like image 326
Doug Avatar asked Feb 01 '12 22:02

Doug


People also ask

What is a Wakelock Android?

A wakelock is a powerful concept in Android that allows the developer to modify the default power state of their device. The danger of using a wakelock in an application is that it will reduce the battery life of a device.

Can foreground service be killed by Android?

The Android system stops a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, it's less likely to be killed; if the service is declared to run in the foreground, it's rarely killed.

What is Android foreground service?

Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources. Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services.

What is the difference between foreground and background service?

For Android Developers, a Service is a component that runs on the background to perform long-running tasks. A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated.


1 Answers

Holding a WakeLock and a foreground Service are not really related and shouldn't be compared are to which direction is best.

Android OS is built to swap out processes based on a variety of factors. This means your process might get killed at any point by Android and it provides a framework to help you, the developer, to ensure your app can save and restore its state when this happens.

A WakeLock simply prevents the CPU from sleeping which helps save battery when the phone is not in use.

Now, a combination of both would help you achieve what you want but at great user cost. I wouldn't want an app in my phone to keep the CPU constantly running or a notification icon to show up constantly in the notification bar (that's what a foreground service does).

Keep in mind, starting a service in foreground mode does not guarantee your app will not get killed. It might still happen albeit rarely.

What is it you are trying to achieve here? Why keep monitoring the devices accelerometer? Perhaps you should only monitor it only when an Activity of your app is in the foreground instead.

like image 166
dnkoutso Avatar answered Sep 28 '22 17:09

dnkoutso