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.
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.
Partial wake locks are a mechanism in the PowerManager API that lets developers keep the CPU running after a device's display turns off (whether due to system timeout or the user pressing the power button). Your app acquires a partial wake lock by calling acquire() with the PARTIAL_WAKE_LOCK flag.
So, what exactly are these? Wakelocks are power-managing software mechanisms, which make sure that your Android device doesn't go into deep sleep (which is the state that you should strive for), because a given app needs to use your system resources.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With