Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 to 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

like image 308
CITBL Avatar asked Nov 05 '22 21:11

CITBL


1 Answers

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();
like image 164
vt. Avatar answered Nov 14 '22 22:11

vt.