Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top