Should I use WakeLock in an IntentService?

Suppose I use BroadcastReceiver to receive events (intentions) that wake up the phone, even if it is in deep sleep mode (for example, an incoming data packet on a socket or an incoming text message). Then I sent the data to the IntentService for processing. Should I use WakeLock?

If I do not use wakelock, can I be sure that the device will not go into sleep mode until the start queue of my service is empty (and therefore the service is stopped)? (Assume processing may take a long time).

If Wake Wake is needed, where and when should I create and use it, and when should I let it go? I would like to immediately release WakeLock after the service’s intent queue is empty.

Thank you in advance

+5
source share
1 answer

The device does not guarantee that it will stay awake long enough to go through the queue of intentions. If it’s really important for you to get things done, it’s best to do something like this in 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();
+2
source

All Articles