What exactly do wake-locks prevent? - android

In the wake-lock training doc it says:
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.
It was my impression that "before the device goes to sleep" referred to doze mode. However, the answer to the SO post Wakelock and doze mode states:
Holding a PARTIAL_WAKE_LOCK is insufficient to block Doze mode
So, if a wake-lock doesn't prevent doze mode, then what exactly is meant by "keep the CPU running in order to complete some work before the device goes to sleep"?
Also, in the doze standby training doc it says:
An app that is partially exempt can use the network and hold partial wake locks during Doze and App Standby.
If (for some reason) "before the devices goes to sleep" does refer to doze mode, then does this mean that wake-locks have absolutely no effect unless you are on the white list for no battery optimizations?
Specifically, I'm talking about partial wake-locks on API 31+.

what exactly is meant by "keep the CPU running in order to complete some work before the device goes to sleep"?
Android devices can power down their CPUs to reduce battery consumption. This usually happens shortly after the screen turns off.
A partial wakelock says "allow the screen to turn off but keep the CPU powered on". This is used for things like long-running audio playback (music, audiobooks, podcasts, etc.).
A full wakelock says "do not allow the screen to turn off either". This is used for things like video players, where the user's expectation is that the screen will stay on despite limited user input.

Related

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.

Android phone , what units are shut off In sleep mode

I have a scenario that the android device exchanges data with a FTP server via Roaming service. I hope to make OS enter proper sleep mode by setting WakeLock type during the data transferring because it really has no interaction with enduser.
I am going through the sleep threads and trying to verifying that what units will be shut down in sleep mode to prevent data sending. So far, no official doc indicates them .
So , Would you please help to enlighten me on the questions: When Android enters sleep mode , what components will stop working , like WIFI , Data Roaming ?
Thanks a lot.
When Android enters sleep mode, almost everything is shut off. Only basic system apps and GSM/CDMA radios are left on to receive texts, calls, push notifications etc. Services, Activities are all shut off, and the CPU is put into a low power mode. Depending on what the user chose in the wifi settings, the wifi connection may or may not switch to mobile data or shut off completely.
If you want to keep processing, but don't need the screen on to interact with the user, you should acquire a PARTIAL_WAKE_LOCK in which the screen and keyboard are off, but the CPU is kept on. However, remember to release the wake lock when you are done, as PARTIAL_WAKE_LOCK is the only one of the four wake locks in which the CPU will continue to run until you release it or your app is killed. Forgetting to release the wake lock can be a huge drain on the user's battery.
When the power off button (lights off) is pressed by user then phone into sleep mode.
Sleep mode means CPU will be in sleep mode and will accept only command from radio interface layer and alarms. Within fraction of seconds the CPU will go into sleep mode.
When we need only CPU on and other things off we use PARTIAL_WAKE_LOCK which suits perfect in your scenario. Weather this lock is generally used in apps like mp3 player etc.
As you want data transfer here in this mode then you have to override already given methods from class PowerManager with specifically by allowing some function to be turned on in this mode.

What is influenced by Android Wakelock?

I am currently developing an app that runs a service from time to time. Currently, the service acquires a wakelock, reads some sensors and sends some information over WIFI (if any). Now what I want to know is weather a wakelock influences sensors and connectivity or not. Is it possible to do these tasks without any wakelock?
Cheers
A wake lock is essentially used to lock the device in an "awake" state, in which the CPU will be on, and the screen may or may not be on.
It is not possible to do these tasks without a wakelock if the phone is in sleep otherwise, as then the CPU is also in sleep mode. However, if the user is using the device for something else, and your app is in the background, you can do these tasks without a wakelock.
Keep in mind that almost everything you're doing is battery intensive (sensors, WiFi, wakelock) and you should not do it too often so that you don't degrade the user's battery life.

what is the difference between Sleep mode and Deep sleep mode?

I would like to know what is the difference between Sleep mode and Deep sleep mode in android phone and what will be the impact of when phone goes to sleep mode and deep sleep mode.
Please provide me the answer in details.
regards,
Piks
Deep sleep mode doesn't have anything to do with hibernate and boot time, as somebody wrote in deleted answer.
You should not avoid the phone sleep, this will drain your battery very fast. To receive messages from server, you should use [C2DM] GCM which has the same basic characteristics as old fashion deprecated C2DM.
More info about the android sleep here:
http://developer.android.com/reference/android/os/PowerManager.html
You can use WAKE_LOCK to prevent device going to deep sleep. But you must take into account battery drain. More info: http://developer.android.com/reference/android/os/PowerManager.html
Deep sleep: turns your phone CPU central processing unit ie brain to the lowest clock cycle speed. On mine it is 200mhz, where max is 1600mhz.
it also disables some sensors it figures you might not be using, such as dependin on phone camera, gyro, etc. some of it gets shut off to save power.
When you wake up your phone depending on governer, it rams cpu clock cycle up to 500mhz, or 800mhz, or 1600mhz, and re activates the sensors, sending power back to the camera, cyro, magnet, etc that your phone may have proxi sensor
This is more or less what deep sleep really IS. program, to save power, by disableing unused sensors, and clocking cpu way down to minimum.
Credits: vrigil kellogg http://forums.androidcentral.com/sprint-galaxy-s-iii/247066-what-deep-sleep.html post#14

Android battery usage keep awake explanation

In the Battery usage screen there is a time for "Keep awake". What does that mean exactly? Is there any documentation somewhere about those numbers?
This appears to show the amount of time that an app has asked the OS to stay in a waking state.
You can see the options available to the developer in the PowerManager class.
For example, an app can request a PARTIAL_WAKE_LOCK. As long as at least one app has requested a partial wake lock, the device will stay active (and consuming battery) even when the screen is off. From the docs:
If the user presses the power button, then the screen will be turned
off but the CPU will be kept on until all partial wake locks have been
released.
I believe the Keep awake time is reporting for how long a given app had this flag set.
Examples on my phone right now,
Google Chrome Beta has an extremely short Keep awake time, 5s. This looks very well behaved.
Another app known to be a terrible battery hog has a Keep awake of nearly 2 hours, despite being actively used for only a few minutes. I would guess this app is not releasing its partial wake lock.
This talk from Google IO on Coding for Battery life kind of clears it a little better:
http://developer.android.com/videos/index.html#v=OUemfrKe65c
It keeps the screen awake while charging. You can check this discussion.
http://www.droidforums.net/forum/team-d1-miui/103349-battery-settings-question-stay-awake.html
You may also note that this time that you talk about seems like the time which this phone uses this function.
I believe it is to keep your screen from sleeping while you recharge your battery.
Here's a relevant topic:
http://www.droidforums.net/forum/team-d1-miui/103349-battery-settings-question-stay-awake.html

Categories

Resources