Android lint warns on the following with [Wakelock]:
public static void acquire(Context ctx, long timeout) {
if (wakeLock != null) {
wakeLock.release();
}
PowerManager powerManager
= (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE,
Common.TAG);
wakeLock.setReferenceCounted(false);
if ( timeout <= 0 ) {
wakeLock.acquire();
} else {
wakeLock.acquire(timeout);
}
}
public static synchronized void release() {
if ( wakeLock != null ) {
if ( wakeLock.isHeld() ) {
wakeLock.release();
}
wakeLock = null;
}
}
It gives the warning for the first occurrence
[lint] [...]/WakeLocker.java: Warning: The release() call is not always reached [Wakelock]
Yet, it does not really need to be released every time, as there is a timeout.
The default solution of wrapping this in a try-catch
-block, as for example in android-wakelock-not-released-after-getactivenetworkinfo, or in @rainash's answer below, do not address the problems that led to using this approach, namely that the device can go back to sleep.
How is this fixed? Or should it be ignored?