Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will android stop its cpu without wake lock?

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.

like image 983
lyu Avatar asked Dec 01 '11 06:12

lyu


People also ask

What would be one reason you would need to acquire a wake lock to stop the device from sleeping?

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.

What is partial wake lock in Android?

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.

What is CPU Wakelock?

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.


1 Answers

Answers to all your sub-questions

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

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

  3. Yes.

like image 144
Eastern Monk Avatar answered Oct 01 '22 21:10

Eastern Monk