Following scenario:
IntentService
gets started from Activity
(so the screen is ON). In this process 1 or many intents
can be sent (which the IntentService
then executes sequentially). This means onHandleIntent
could run 1 time, but it could also run 20 times.
In this scenario, should a Wakelock
be acquire
d and release
d in onHandleIntent
or in onCreate
/onDestroy
?
If the Wakelock
is acquire
d in onHandleIntent
and release
d at the end of this method, can the device fall asleep in the moment before the next intent
is started?
//THIS COULD RUN 20 TIMES IN A ROW
@Override
protected void onHandleIntent(@Nullable Intent intent) {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
, "WakeLock:");
wakeLock.acquire();
//do a few seconds of blocking work
wakeLock.release();
//can the device fall asleep before the next onHandleIntent?
}