Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to release a wake lock?

I am setting a wake lock using wl.acquire(). This works fine. I need that wake lock as long as my application lives, so calling release() can only be done when the application is left.

Do I have to call release() somewhere? For example in onStop()? I would say no, but I am not sure.

like image 253
AndyAndroid Avatar asked Jan 26 '11 08:01

AndyAndroid


People also ask

How do you release a Wakelock?

To release the wake lock, call wakelock. release() . This releases your claim to the CPU.

What is a wake lock on my phone?

Wakelocks are power-managing software mechanisms, which make sure that your Android device doesn't go into deep sleep (which is the state that you should strive for), because a given app needs to use your system resources.

How do I wake up my lock screen?

This feature can also be used to lock your phone—double-tap to lock, double-tap to unlock. Alternatively, if double-tapping your screen to lock and unlock your Android still isn't working, you can use a third-party app like Screen Off. Screen Off adds a handy shortcut on your phone's home screen.

What is a wake lock Chromebook?

The Screen Wake Lock API provides a way to prevent devices from dimming or locking the screen when an application needs to keep running.


2 Answers

if you refer yourself to these 2 pages:

http://developer.android.com/reference/android/os/PowerManager.html

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html#release()

You should release the wake lock as soon as you can: therefore if your app is closing/pausing release it!

Also, word of advice, make absolutely sure you need a wake lock, when you need it and for ho long.

in my case I put one only for the in game screen to avoid the users screen going black while thinking (since it is an opengl app it takes a few seconds to fully load), but in the other views I release it.

Trust me when I say that it is annoying for a user to end up with a empty battery because an app was forcing itself to stay awake.

like image 68
Jason Rogers Avatar answered Oct 04 '22 06:10

Jason Rogers


Beside the others useful answers you already received, I just found this interesting answer about how to force screen on, maybe you'll find it useful too, if you don't really need a wake lock.

like image 23
Adinia Avatar answered Oct 04 '22 06:10

Adinia