Relation between Foreground Services and PARTIAL_WAKE_LOCK - android

I know foreground services have "unlikely to kill" behavior. According to Android documentation:
It is still theoretically possible for the service to be killed under
extreme memory pressure from the current foreground application, but
in practice this should not be a concern.
I think this solves the low memory concern for most cases. But I cannot find any documentation on whether the CPU goes to sleep while a foreground service is running.
Does a foreground service automatically acquire PARTIAL_WAKE_LOCK or one has to call it explicitly if needed?
Is there any way to check/log which application/services are using PARTIAL_WAKE_LOCK?

Android foreground service and PARTIAL_WAKE_LOCK have nothing to do with each other.
Foreground service -> it tells the OS to put your service in the highest priority queue. If OS needs memory for any reason, it will kill your service at LAST, and it will start with queue at lowest priority (I believe there are 5 priority queues, and foreground is the highest).
PARTIAL_WAKE_LOCK -> it tell the OS not to put the CPU to sleep when device goes to sleep (whenever this happen).
So, even if you have a foreground service, if device goes to sleep the cpu might go to sleep as well (it might not if some other app acquired a PARTIAL_WAKE_LOCK).
I don't think there is a way to check which applications acquired wake lock programmatically, but you can plug your device to DDMS and you will see PowerManager logs when someone acquires a wake_lock.
Hope it helps !

Related

Services and wake lock

I have some questions concerning wake lock and services
1- I try to test my service when the screen is off and not acquiring the wake lock, i was expecting that system will kill my service but it didn't happen, so what is the purpose of wake lock?
2- I want to know when then the system goes into doze mode, is it when i turn off the screen or after some time of turning it off? and what happens to my service in this case? and how to know that the system is in doze mode?
3- I know that since Android O normal background service will be killed after nearly one minute, i tried to test that by making intent service and make it running for more than one minute, it was already killed but started again and continued execution, so what is the purpose of killing it and starting it again?
4- does doze mode affect foreground service? and should i acquire wake lock in case of foreground service or is it acquired by default?
Code of Intent service
Logcat
I know they are lots of questions but i am confused with these topics
thanks in advance
The device may fall asleep if the user is inactive and nothing is keeping the device awake. A WakeLock is used to ensure the device stays awake.
You may check those links for additional information: Good answer Official Documentation
Information about Doze mode, Standby and some other things that you may be interested in:
link
Background Service Limitations: While an app is idle, there are limits to its use of background services. This does not apply to foreground services, which are more noticeable to the user.
link
Processes which have a current running foreground service are supposed to be unaffected by Doze. Bound/unbound, started/not-started, and wakelocks do not affect this whitelisting process.
link
--- Update ---
Some things could change from the moment of those questions were asked, so prefer to read documentation or search for the actual information about it. Also it's a good idea to check information about modern solutions for back ground like WorkManager.

Does service.startForeground imply wakelock?

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.
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.
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

Android long time running service

I am working on an Android project and I need the app to work even when the device is locked.
The idea is to open the app that will start the (Intent)Service, the service processes the data all the time. The device can be locked/put away and after some time when the app is opened the service is manually stopped. The service should be running all the time in the background.
I have found information online, but I am not sure what to use and in which way..
I have found that the IntentService can be used. Also the service should run in a new thread. I need to process the data from gps all the time, should I use WakefulBroadcastReceiver?
Thank you.
IntentService is not necessarily what you want to use. It will automatically spawn a new thread just to handle an incoming Intent. Once all incoming Intents have been handled it will stop the Service. To have a long running Service, you would need to derive from Service and when it is started return START_STICKY from the onStartCommand() method, plus spawn your own thread to handle your background work.
If you need to monitor GPS, you'll have to manage that along with keeping the device awake using a WakeLock. Note that in Marshmallow, this gets more complicated because of the new Doze mode where even wakelocks are ignored.
Also, note that the way Android is architected there is still a chance that your application running the background Service may be killed. Android uses a unique process management technique based on memory pressure and user perceived priority to determine how long a process should stick around. I recommend reading up on the Service lifecycle in the documentation.
In android their is no fool proof way to ensure that your service runs forever because the LMK(low memory killer) when the system needs resources (based on a certain memory threshold) , kills the service then if it can restarts it. If you handle the restart properly the service will continue to run.
Services that are given foreground priority are significantly less likely to be killed off, so this might be your best bet. However their will be a notification of your service running the in the background on the menu bar up top. Foreground Service

Android: When is WakeLock needed?

If I have an IntentService that simply updates the SharedPreference, is a (partial) WakeLock needed?
I understand that a WakeLock keeps the CPU awake, but when is it needed?
If you need to keep the CPU running in order to complete some work before the device goes to sleep, you can use a PowerManager system service feature called wake locks. Wake locks allow your application to control the power state of the host device.
Creating and holding wake locks can have a dramatic impact on the host device's battery life. Thus you should use wake locks only when strictly necessary and hold them for as short a time as possible. For example, you should never need to use a wake lock in an activity.
One legitimate case for using a wake lock might be a background service that needs to grab a wake lock to keep the CPU running to do work while the screen is off. Again, though, this practice should be minimized because of its impact on battery life.
Unfortunately, some poorly-coded, malicious, or simply buggy apps might create an abnormal amount of undesirable wakelocks. Other apps require constant Internet access in order to operate in a normal fashion - Facebook and Messenger are probably the most popular representatives. They persistently request information from the web (the so-called "polling" for new events), which is causing subsequent wakelocks.
In other cases, an update to a given app can also cause certain issues, which usually result in partial wakelocks. The latter keep your CPU constantly humming in the background, sometimes without your knowledge, and prevent your device from "going to sleep". That's a pretty substantial prerequisite for anomalous battery drain. Thus, it is advisable to regularly monitor the wakelocks on your device and see which of your apps go harsh on our system's resources.
Read more at:
What-are-wakelocks-how-they-affect-the-battery-life-of-your-Android-device-and-how-to-Greenify
Reference: https://developer.android.com/training/scheduling/wakelock.html
It is needed when you don't want CPU to sleep when user locks the screen for example.
If you have an IntentService without acquired WakeLock it will pause after a while if user locks the screen and it will continue its work when user wakes a device. With WakeLock acquired your service will work even if the screen is locked.
As #My God mentioned, it impacts on battery life a lot, so, use it only when you really need to finish some operation and you cannot wait till user wakes a device.

When will a Service be put to sleep?

The rules for holding cpu partial wake locks are very vague. Can someone explain when and why we should acquire one? When can the system decide the service should be suspended while doing some work on a thread?
I would like to understand the details of the cpu scheduler / activity / service manager.
Is the best place the android source code? if so which package?
Can someone explain when and why we should acquire one?
You acquire one when you need the CPU to stay running to complete some bit of work. You release it when that work is done.
So, for example, suppose you have an IntentService triggered by AlarmManager. You want the IntentService to check for new email messages for your email client. You would want to acquire a WakeLock so the CPU will not fall asleep while you are in the middle of doing that, then ensure that you release it. This sort of pattern is why I created the WakefulIntentService.
When can the system decide the service should be suspended while doing some work on a thread?
The fact that you are doing "some work on a thread" is immaterial. The CPU will fall asleep based on user inactivity. If the user is not keeping the device awake, and you are not keeping the device awake with a WakeLock, the device will fall asleep.
Having the CPU fall asleep is very important for power consumption. If you, say, decide that you are going to keep the CPU running 24x7, the battery will run dry relatively quickly.

Categories

Resources