将处理程序,当CPU睡觉postDelayed没有被解雇? [英] Will a Handler postDelayed not being fired when CPU sleeps?

查看:434
本文介绍了将处理程序,当CPU睡觉postDelayed没有被解雇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些处理程序正在间隔不超过5分钟执行的活动。该活动是由的BroadcastReceiver 启动,并可能与屏幕关闭等待用户抓起电话,并得到用户的输入,遇到这种情况时,活动<$启动C $ C>的onPause()被调用,所以可以肯定的CPU处于睡眠模式。我没有设置活动开启屏幕上,因为我想尽可能多的电池保存成为可能。

I have an activity with some Handlers that are being executed at intervals no more than 5 minutes. The activity is launched from a BroadcastReceiver and may be launched with the screen off waiting for the user to grab the phone and get the user input, when this happens, the activity onPause() is called so for sure CPU is in sleep mode. I have not set the activity to turn screen on because I want to save as much battery as possible.

我已经与我的手机进行了测试和作品真的很好,而屏幕是关闭所有的处理程序执行所有的code也得跑。如果我打开屏幕打开和关闭,而该活动是开放的一切工作正常。

I've tested it with my phone and works really well, while screen is off all Handlers execute all the code they have to run. If I turn the screen on and off while the activity is open everything works fine.

现在,我读过到处都是,由于当屏幕熄灭,CPU睡觉,大部分时间用加速度计预期某些设备无法正常工作。现在的问题是,我需要获得一个 WakeLock ,因此CPU不睡觉,而我的活动是开放的?我真的很想知道这是因为,正如我之前所说,我不通过收购昂贵的要浪费电池 WakeLock

Now, I've read everywhere that some devices does not work as expected when screen goes off and CPU sleeps, most of the times with accelerometers. Now my question is, do I need to acquire a WakeLock so the CPU does not sleep while my activity is open? I really want to know this because, as I said before, I don't want to 'waste' battery by acquiring an expensive WakeLock.

我会preFER一个人真正知道它是如何工作的详细的解答。

I would prefer a detailed answer of a person that really knows how this works.

推荐答案

你的目标不能stabily用你的方法来实现。如果您使用的处理器的postDelayed()方法和CPU进入DEEPSLEEP时,MS计数器将停止,只会继续如果CPU再次醒来。

Your goal cannot be stabily achieved by your approach. If you use an Handler's postDelayed() method and the cpu goes to deepsleep, the ms counter will stop and will only continue if the cpu wakes up again.

更多详细信息请参见本。

所以,如果你想有某种cron作业,你将需要使用wakelock的。 幸运的是有一个服务实现,正是这么做的:清醒IntentService

So if you want to have some kind of a cron job you will need to use wakelock. Fortunately there is a Service implementation that does exactly that: Wakeful IntentService

从DOC:

有关Android的相当于cron作业和Windows的计划任务的推荐方式是使用AlarmManager。这种运作良好,再加上IntentService时,因为该服务将完成它的工作在后台线程并关闭时,有没有更多的工作要做。还有一个小问题:IntentService什么也不做,以保持设备清醒。如果报警是一个唤醒变种,这款手机将只保持清醒自己,而BroadcastReceiver的处理报警是在其的onReceive()方法。否则,手机可能会回落睡着了。 WakefulIntentService试图通过易于IntentService的结合部分WakeLock打击这种。

The recommended pattern for Android's equivalent to cron jobs and Windows scheduled tasks is to use AlarmManager. This works well when coupled with an IntentService, as the service will do its work on a background thread and shut down when there is no more work to do. There's one small problem: IntentService does nothing to keep the device awake. If the alarm was a WAKEUP variant, the phone will only stay awake on its own while the BroadcastReceiver handling the alarm is in its onReceive() method. Otherwise, the phone may fall back asleep. WakefulIntentService attempts to combat this by combining the ease of IntentService with a partial WakeLock.

这篇关于将处理程序,当CPU睡觉postDelayed没有被解雇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆