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.
Related
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.
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.
I know android use wake lock to keep cpu running or screen on.
It's obvious that screen wake lock prevents the user active timer from turning off the screen after a period of user inactivity.
But I'm wondering when exactly will the cpu wake lock take effect.
1.If I create a new thread and keep draining cpu in background with out any wake lock, turn off the screen will not stop it. Will it stop and when will it stop?
2.What about a thread scheduled with Timer.schedule()?
3.It leads to another question, if I keep a long socket connection in a service, which is blocked at socket.read(). Do I have to acquire a wake lock to make sure the service will be wakeup when the socket receives any data form remote?
Thanks.
Answers to all your sub-questions
Android sleeps when no wake-lock is active. It does not matter what processes and threads are running it will still sleep. That means if your thread or some other process out there has not activated a wake lock your thread will not execute and hence will not drain any battery. The thread will be made active only when some other process acquires a wakelock.
Same is applicable to the Timer.schedule(). Say you write a Timer that executes every second but without any wake-lock, and say android goes to sleep for 10 seconds. When it wakes p on 11th second it will identify that your timer has expired 10 times it will simply discard the9 instances and execute it only once. If you want very reliable timers you will have to either obtain a wake lock or user AlarmTimer.
Yes.
What i learn from different functionality related to Wake-Lock , Android System will never sleeps, if it sleeps you will not get SMS , Call i.e Android will not run only OS level task when it goes on sleep.
Wake-lock is a mechanism where any application can request system to have the device stay on for him. Application can perform operation on background despite user haven`t move to launch that application.
For 1 & 2 Akshar has explained correctly.
3. As to perform any operations in your application while system is in sleep state(only OS level task are runnning) , you first have to request wake-lock from system and then only application can run its operations. After completing operations you should release the wake lock so that system can move to managing OS level tasks.
I want to prevent Android Mobile from going into sleep mode when my threads are sending HTTP request. as what happened while threads are doing HTTP calls mobiles goes to sleep mode and when user wakes up the phone threads never complete.
User has to restart the app. what to do? please help
You need a WakeLock. There are different kinds of wake locks so be sure to select the least aggressive one that meets your needs. In particular it sounds like you need a Partial Wake Lock.
Partial Wake Lock - Wake lock that ensures that the CPU is running. The screen might not be on.
Also, make sure you add the permission android.permission.WAKE_LOCK to your manifest. And finally be double sure to Release your lock when you are done.
To prevent the phone from sleeping you can use a WakeLock but you should be careful when doing this to not kill the user's battery. If the phone goes to sleep does the user really care if your app finishes the requests it was making?
You will also need the WAKE_LOCK permission.
for JAVA: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
for KOTLIN: window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Use this code, it will keep your device wake up and you didn't need any permission.
I have an app that needs to send a periodic heart beat to a server, but when the phone goes into standby mode the background heartbeat thread dies. Is there anyway to wake the phone from standby, send the heartbeat and then go back to sleep programmatically? I want to avoid using PARTIAL_WAKE_LOCK if possible.
Thanks
Is there anyway to wake the phone from standby, send the heartbeat and then go back to sleep
programmatically?
Use AlarmManager with a _WAKEUP-style alarm. Here is a sample project illustrating its use (along with a WakefulIntentService you will want, to make sure the device does not fall back asleep during your network I/O).
I want to avoid using PARTIAL_WAKE_LOCK if possible.
You cannot do network I/O without a WakeLock, because the device will fall back asleep during the I/O. Using AlarmManager, you can arrange to only hold a WakeLock during the actual heartbeat work, not 100% of the time.