开发者

What events can wake up a sleeping Android device?

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 i开发者_运维问答s 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?


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:

  • https://github.com/commonsguy/cwac-wakeful
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜