Cemre Cemre - 1 month ago 13
Android Question

Android wake lock is not released

I get the wake lock like in

onCreate
method and it works fine (I also set the permissions):

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Wake lock");
wl.acquire();


However, when I try to release it like:

@Override
protected void onDestroy()
{
super.onDestroy();
wl.release();
}


it is not released for some reason. How is this possible ? Any ideas ?

EDIT: I have an exit button which calls finish() which then calls onDestroy(). When I press the exit button and then put the phone to sleep I expect my program not to work but it works which shows me that the lock is not released

Answer

The right way to determine whether your WakeLock-releasing code works is to use adb shell dumpsys power, before and after the release, to see if your WakeLock is released. Since other apps can request and acquire WakeLocks whenever they want, other casual checks (e.g., does my app run when the screen is off?) will be unreliable.

Comments