哪些事件可以唤醒正在睡眠的 Android 设备?

发布于 2024-10-31 12:14:33 字数 811 浏览 4 评论 0原文

我正在编写一个 Android 客户端应用程序,它使用 HTTP 长轮询(Comet)不断接收来自服务器的推送通知。

我不想总是保留 WakeLock,因为它会耗尽电池,但我需要确保设备即使处于睡眠模式也能收到通知。

我发现了这个问题:

Android: Android 设备上的睡眠阶段/级别?

其中“CommonsWare”提到非 Wifi 套接字上的传入数据包将唤醒设备。

所以我的解决方案看起来像这样:

    Client ------------------------- Server
              ---- Request----->
    release WakeLock                 (Allow device to sleep)





              <----Notification--    (Hopes it can wake up the device)
    require WakeLock
    process the notification  
              ---- Request----->
    release WakeLock          
    ....

但是在接收通知和需要唤醒锁之间有一点时间窗口,所以我的问题是,设备将保持这种唤醒状态多长时间?设备是否可以在这段时间内重新进入睡眠状态?

I am writing a Android client app which keeps receiving push notifications from a server using HTTP long-polling(Comet).

I don't want to always keep WakeLock since it will drain battery, but I need to make sure the device can receive notification even when it is in sleep mode.

And I found this question:

Android: Sleep stages/levels on an Android device?

where "CommonsWare" mentioned that an incoming packet on a non-Wifi socket will wake up the device.

So my solution looks like this:

    Client ------------------------- Server
              ---- Request----->
    release WakeLock                 (Allow device to sleep)





              <----Notification--    (Hopes it can wake up the device)
    require WakeLock
    process the notification  
              ---- Request----->
    release WakeLock          
    ....

But there is a little time window between receiving the notification and requiring the wakelock, so my question is, how long will the device keep this awake state? Is it possible for the device to back to sleep during this time window?

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

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

发布评论

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

评论(1

有木有妳兜一样 2024-11-07 12:14:33

设备将处于唤醒状态足够长的时间,以便在 BroadcastReceiver 中执行一些简短的代码。我无法找到确切的毫秒数,但想法是,在您的接收器中,您应该获取继续进行自己的处理所需的任何 WakeLock

然而,WakeLock 的精确管理可能很棘手。我建议使用@CommonsWare的WakefulIntentService

The device will be awake for long enough to execute some short code in the BroadcastReceiver. I have not been able to find an exact number of millis, but the idea is that in your receiver, you should grab whatever WakeLock you need to proceed with your own processing.

However exact management of the WakeLock can be tricky. I recommend using @CommonsWare's WakefulIntentService:

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