问题:
>我假设WakeLock是某种类型的锁,当执行代码保持时,它会阻止设备休眠.如果设备已处于睡眠/待机模式,那么代码会执行吗?假设它永远不会获得WakeLock?
>当在后台线程(AsyncTask)中完成一个长时间运行的任务(abt 7-8秒)时,我是否应该为持有WakeLock而烦恼? AsyncTask是否已经为我获取了它?
>赞赏官方文件的链接和唤醒锁上的文字.
谢谢.
解决方法
从AlarmManager类文档:
The Alarm Manager holds a cpu wake
lock as long as the alarm receiver’s
onReceive() method is executing. This
guarantees that the phone will not
sleep until you have finished handling
the broadcast. Once onReceive()
returns,the Alarm Manager releases
this wake lock. This means that the
phone will in some cases sleep as soon
as your onReceive() method completes.
If your alarm receiver called
Context.startService(),it is possible
that the phone will sleep before the
requested service is launched. To
prevent this,your BroadcastReceiver
and Service will need to implement a
separate wake lock policy to ensure
that the phone continues running until
the service becomes available.
2.如果您正在使用AsyncTask,则需要在onPostExecute()
上将结果发布到UI线程
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
3.我建议您阅读Power Manager的官方文档,其中介绍了WakeLock概念.