开发者

Should I use WakeLock in IntentService?

Suppose I use BroadcastReceiver to receive events(intents) that awake the phone even if it is in deep sleep mode (such as incoming data packet on a socket, or incoming text message). Then I forward received data to an IntentService for processing. Should I use WakeLock?

If I do not use wakelock, can I be sure that the device will not go t开发者_运维知识库o sleep mode until the intent queue of my service is empty (and hence the service is stopped)? (Suppose processing can take long time).

If WakeLock is required, then where and when should I create and aquire it, and when should I release it? I would like to release WakeLock immediatlely after the intent queue of the service is empty.

Thank you in advance


The device is not guaranteed to stay awake long enough to get through the intent queue. If it's really important that you handle intents promptly, your best bet is to do something like this in your onHandleIntent:

mWakeLock.acquire();  // mWakeLock should be set to be reference counted

// (do work)

mWakeLock.acquire(1000);  // hopefully long enough to get to the next item
mWakeLock.release();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜