IntentService - Acquiring Wakelock in onCreate vs

2019-06-14 11:08发布

问题:

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 acquired and released in onHandleIntent or in onCreate/onDestroy?

If the Wakelock is acquired in onHandleIntent and released 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?
}