android – 初学者问题:Wakelocks

前端之家收集整理的这篇文章主要介绍了android – 初学者问题:Wakelocks前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是WakeLock概念的新手,需要你的帮助.

问题:

>我假设WakeLock是某种类型的锁,当执行代码保持时,它会阻止设备休眠.如果设备已处于睡眠/待机模式,那么代码会执行吗?假设它永远不会获得WakeLock?
>当在后台线程(AsyncTask)中完成一个长时间运行的任务(abt 7-8秒)时,我是否应该为持有WakeLock而烦恼? AsyncTask是否已经为我获取了它?
>赞赏官方文件链接和唤醒锁上的文字.

谢谢.

解决方法

1.如果手机处于完全睡眠模式,除了来电之外,您可以使用AlarmManager意图唤醒手机.

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文件

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概念.

原文链接:https://www.f2er.com/android/315164.html

猜你在找的Android相关文章