When will android stop its cpu without wake lock?

走远了吗. 提交于 2019-12-06 00:39:01

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8337418/when-will-android-stop-its-cpu-without-wake-lock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!