Using Partial WakeLock in libGDX

A reader asking how they could keep sound running in a libGDX Android project when the screen turns off. I don’t use libGDX in Pocket Fan, but I do use a Wake Lock┬áto keep the sound running while the screen is off, so maybe we can do something similar with libGDX.

Pocket Fan grabs a WakeLock like so:

PowerManager mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock mWakeLock =

The partial wake lock will keep the CPU awake, while allowing the screen to turn off. Here’s the problem with trying to do that in libGDX:

protected void createWakeLock (AndroidApplicationConfiguration config) {
	if (config.useWakelock) {
		PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "libgdx wakelock");

As you can see, when you use libGDX the way it was meant to be used, i.e. writing your game code in another project and creating a lightweight Android project that only calls

initialize(ApplicationListener listener, AndroidApplicationConfiguration config)

from your Activity’s onCreate method, you will only be able to use the full wake lock. You can remedy this by overriding createWakeLock in your main Activity, like so:

protected void createWakeLock (AndroidApplicationConfiguration config) {
	if (config.useWakelock) {
		PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "libgdx wakelock");

This will allow you grab a partial wake lock, which will allow the screen to turn off.

Pitfalls that I can see cropping up:

  • When the screen turns off, your activity’s onPause() method is called. You should probably only do your normal turning off sounds and stopping calculations if you don’t hold the wake lock.
  • This could drain your users’ battery much more quickly if you are not careful.

It’s worth noting that they coded the FULL_WAKE_LOCK in for a reason. When you are making a game, the screen probably shouldn’t turn off. I’m not sure how libGDX will behave if you grab a partial wake lock. You’ll have to do some testing to see what your game does when ApplicationListener.pause() is called when the screen turns off.

Did I miss anything? Did that code not work? Let me know in the comments.

2 comments on “Using Partial WakeLock in libGDX

  1. Nah I’m just getting Force close errors, think I will stick with the dim screen wake lock :)
    It’s not that big an issue and I have many other things to sort out!
    Thanks for doing that though :)
    I might try out that pocket fan app!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>