Webview behaviour wakelock android - android

How WebView works with wake lock?
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright
*If you hold a partial wake lock, the CPU will continue to run, regardless of any display timeouts or the state of the screen and even
after the user presses the power button. In all other wake locks, the
CPU will run, but the user can still put the device to sleep using the
power button.
In addition, you can add two more flags, which affect behavior of the screen only. These flags have no effect when combined with a PARTIAL_WAKE_LOCK.
This two FLAGS:
ACQUIRE_CAUSES_WAKEUP Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once
it turns on (e.g. from user activity). This flag will force the screen
and/or keyboard to turn on immediately, when the WakeLock is acquired.
A typical use would be for notifications which are important for the
user to see immediately.
ON_AFTER_RELEASE If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to
remain on a bit longer. This can be used to reduce flicker if you are
cycling between wake lock conditions.
Scenario: App running one webview that need wait for onPageFinished() to do the processing (And yes, I need to use wake lock, this is just the simplest scenario)

WebView doesn't aquire wakelocks by itself, it's all up to your app's code.

Related

What exactly do wake-locks prevent?

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.

How to keep screen state with using Wakelock?

I used Wakelock in my application for fire CPU on when the device went to sleep but i
don't want to turn on when screen is off, i mean, i want to keep state of screen and
just turn on cpu for my background works.
I used below code but in some of devices, in wakelock device, the screen was
turn on but as i readed about PowerManager and i realised, i had to used just
PARTIAL_WAKE_LOCK. Is that true?
before code:
wakeLock=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE,"aqs_wake_lock");
after edit:
wakeLock=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"aqs_wake_lock");
A wake lock is a mechanism to indicate that your application needs to have the device stay on
PowerManager defines various types of wakelocks
The following wake lock levels are defined, with varying effects on system power. These levels are mutually exclusive - you may only specify one of them.
As you can see from the picture partial wakelocks will continue to run irrespective of state of screen.

Is wake lock commutative?

Let's say an Android application acquires a wake-lock, and then launches another application by sending it an explicit intent. Does the effect of acquisition of wake-lock last while the other application is in the foreground ?
As described in Keeping the Device Awake it's perfectly natural for a background application to grab and hold a CPU wakelock:
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.
This action is pretty common. For example, imagine a music playing application. Even though the screen is off, or some other activity is in the foreground, it's fine for a background application to hold a wake lock to keep playing music.
Although that last line should really take a warning. As described in Wakelocks and Battery Drain those things tend to burn through battery pretty fast; and worse yet, is that it's a pretty common problem to not release them properly, and end up putting the device into a sleepless mode, where it never goes to sleep.

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 and why PARTIAL_WAKE_LOCK is required?

All other wake locks makes sense e.g. developer want screen not to go off while performing something. But in partial wake lock documentation says that :
If you hold a partial wake lock, the CPU will continue to run,
regardless of any display timeouts or the state of the screen and even
after the user presses the power button. In all other wake locks, the
CPU will run, but the user can still put the device to sleep using the
power button.
Does that mean while performing some operation CPU can go off? Is it required to acquire wake lock in a service?
Does that mean while performing some operation CPU can go off? Is it required to acquire wake lock in a service?
Of course (and here - for the power off button) ! Things are even more complicated if you are trying to start your service while the device is asleep. You most probably won't make it. Have a look at Commonsware WakefulIntentService - the notion is that using an alarm manager Receiver (which holds a wake lock) you must afterwards get a (static) wakelock while still in onReceive() to keep awake.

Categories

Resources