IntentService-在onCreate和onHandleIntent中获取Wakelock

时间:2018-11-15 15:28:58

标签: android intentservice wakelock android-intentservice android-wake-lock

以下情况: IntentServiceActivity开始(因此屏幕打开)。在此过程中,可以发送 1个或许多 intents(然后IntentService会依次执行)。这意味着onHandleIntent可以运行 1次,但也可以运行20次。

在这种情况下,应该在Wakelock还是acquire / release中将onHandleIntentonCreate中的onDestroyWakelock中? >

如果在此方法的结尾acquireonHandleIntent中的release d和intent d,则设备可以在下一个{{ 1}}开始了吗?

//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?
}

0 个答案:

没有答案