AFAIK a process killed takes its locks with it.
Correct.
Or is this an impossible scenario ?
It's fairly unlikely, but certainly not impossible.
In view of the previous should (flags & START_FLAG_RETRY) be added ?
That should be covered by START_FLAG_REDELIVERY
. AFAIK, with START_REDELIVER_INTENT
, there is no RETRY
without REDELIVERY
. If you have evidence to the contrary, I'd love to see it.
Why the if (!lock.isHeld()) check ?
Calling release()
on a WakeLock
that is not held results in an exception. This is just a safety blanket to ensure we don't wind up throwing an unnecessary exception. In theory, it should never be needed; in theory, I should have hair.
Why is this.getApplicationContext() needed ? is not this enough ?
We create a WakeLock
, which we hold in a static data member. Probably the getSystemService()
call does not wind up putting the Context
that called it inside the PowerManager
. And, even if it did, probably the Context
would not be passed to the resulting WakeLock
instance. However, to be safe, by using getApplicationContext()
, we obtain the WakeLock
in a fashion that ensures that the only Context
we could possibly "leak" is the singleton application context, which, as a singleton, is effectively pre-leaked. :-)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…