user user - 1 year ago 108
Android Question

Must every WakeLock.release() call always be reached? android lint warning

Android lint warns on the following with [Wakelock]:

public static void acquire(Context ctx, long timeout) {
if (wakeLock != null) {

PowerManager powerManager
= (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |

if ( timeout <= 0 ) {
} else {

public static synchronized void release() {
if ( wakeLock != null ) {
if ( wakeLock.isHeld() ) {
wakeLock = null;

It gives the warning for the first occurrence

[lint] [...]/ 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
-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?

Answer Source

As @tyczj hinted:

[Lint] also flags issues that may or may not be a problem depending on the context.

So think about the warning, and

If you've manually verified that an issue is not a problem, you may want to mark the issue as verified such that lint does not keep pointing it out.

To remove the warning in this case, use

public static void acquire(Context ctx, long timeout) {


import android.annotation.SuppressLint;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download