AlarmManager 和 Handler+WakeLock 之间哪个最好?

发布于 2024-12-08 05:06:53 字数 932 浏览 3 评论 0原文

我正在为我的 Android 服务使用一个处理程序,该处理程序每​​ 60 分钟(1 小时)重复执行一些操作,并使用 PartialWakeLock 来保持手机不休眠。但这会导致大量电池使用。

因此,我决定研究一下 AlarmManager(我是菜鸟),有人写在这里以求完美对于这种事情..

但是现在在网上阅读我发现谁使用AlarmManager,仍然需要一个WakeLock。这是真的吗?

每 60 分钟(1 小时)运行一个周期而不耗尽电池的最佳方法是什么?

谢谢

AlarmManager Android 开发人员

只要警报存在,警报管理器就会保持 CPU 唤醒锁定 接收者的 onReceive() 方法正在执行。这保证了 在您处理完广播之前,手机不会休眠。 一旦 onReceive() 返回,警报管理器就会释放此唤醒锁。 这意味着在某些情况下,一旦您的手机进入睡眠状态,手机就会进入睡眠状态。 onReceive() 方法完成。如果您的报警接收器打来电话 Context.startService(),有可能手机会休眠 在启动请求的服务之前。为了防止这种情况发生,您的 BroadcastReceiver 和 Service 需要实现单独的唤醒 锁定策略,以确保手机继续运行,直到 服务可用。

但是看起来我需要 2 个唤醒锁,而不是使用处理程序只需要 1 个唤醒锁......这是真的吗?

I'm using for my Android Service an Handler that reapeat some operation each 60 minutes (1 hour), with a PartialWakeLock to keep the phone not sleeping. But this cause a lot of battery usage.

So a decided to study about AlarmManager (i'm noob) that someone wrote here to be perfect for this kind of things..

But now reading along the web i find that who uses AlarmManager, still need a WakeLock. Is it true?

What is the best way to run a cycle each 60 minutes (1 hour), without kill the battery?

Thanx

P.S.

AlarmManager Android Developer

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.

But so seems that i need 2 wakelock vs just 1 wakelock using handler....is it true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

复古式 2024-12-15 05:06:53

我做了很多测试,结果如下:

- 报警管理器比使用处理程序+唤醒锁进行长时间定时操作节省更多电池。

但是您必须对由警报启动的活动/服务使用额外的唤醒锁,因为警报管理器唤醒锁不涵盖它。

即使这种方法使用两个 WakeLock,电池似乎也更高效且寿命更长!在测试期间(2 天),AlarmManager 使用的电池比其他方法少 6 倍。就我自己而言...

希望这可以帮助某人!

I have made many test and this is the result:

-Alarm Manager save more battery than using handler+wakelock for long timing operation.

But you must use an additional wake lock to your activity/service started by the alarm, because the alarm manager wake lock doesn't cover it.

Even of this method uses two WakeLock the battery seems to be more efficient and with more life! During the tests (2days) the AlarmManager use 6 time less battery than other method. In my own case...

Hope this can help some one!

不念旧人 2024-12-15 05:06:53

我建议您使用 AlarmManager 来处理间隔为 1 小时的事件。

因为我们不确切知道您要实现什么目标,所以我们无法提供更深入的答案/建议,抱歉。

I suggest you to use AlarmManager to handle events with 1 hour interval.

Because we don't know exactly what you what to achieve we can't provide a more in deep answer/suggestion sorry.

情话墙 2024-12-15 05:06:53

我不确定它是否仍然相关,

但答案是:首选使用 AlarmManager。您只需要一个 WakeLock 即可在 AlarmManager 唤醒手机并向接收器发送 Intent 后保持手机运行,直到服务完成其工作。因此,与“一直”相比,手机只会唤醒几毫秒。

I am not sure if it is still relevant,

but the answer is: using AlarmManager is preferred. You only need a WakeLock to keep phone running after AlarmManager has woken it up to send an Intent to your receiver and until service has finished its work. So phone will be awake only for a couple of milliseconds, compared to "all the time".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文