In WakefileIntentService.java from the CWAC Wakeful library the code contains:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PowerManager.WakeLock lock = getLock(this.getApplicationContext());
if (!lock.isHeld() || (flags & START_FLAG_REDELIVERY) != 0) {
lock.acquire();
}
super.onStartCommand(intent, flags, startId);
return(START_REDELIVER_INTENT);
}
Why do the code check for START_FLAG_REDELIVERY
- what prevents the following scenario?
onStartCommand()
is called and the lock is acquired.START_FLAG_REDELIVERY
, causing another acquire()
call on the already held lock.release()
once.what prevents the following scenario?
Your scenario implies that Android would terminate the service and leave a WakeLock
outstanding. I am aware of no scenario under which this will occur. Android terminates processes, not services, and it is the OS's responsibility to release any acquired WakeLock
at that point.
It seems strange since START_FLAG_RETRY is not handled, I've opened the issue https://github.com/commonsguy/cwac-wakeful/issues/10 for that.
As I noted in that issue, while START_FLAG_REDELIVERY
has decent documentation, START_FLAG_RETRY
does not. I have no idea when it will get used. I have no idea what the state of the WakeLock
will be based upon those undocumented causes. And so forth. It is far better to risk the occasional accidental sleep than it is to accidentally keep the CPU powered on indefinitely.