Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep screen on during android game without user interaction

I'm using Libgdx for my android game. The game runs fine, but it will automatically pause while running if there is no interaction from the user. How can I avoid the game pause?

My main class impelments AndroidApplication and my game class implements ApplicationListener.

like image 365
user2141514 Avatar asked Jun 04 '26 11:06

user2141514


1 Answers

While JRowan's answer is good for general Android applications, and can be used with Libgdx, you can also use the Libgdx support for acquiring the wakelock. See useWakelock in AndroidApplicationConfiguration. Libgdx will keep track of the wakelock for you. You still need the appropriate permissions in your manifest, though.

Specifically, in your class that extends AndroidApplication:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useWakelock = true;

    initialize(new MyGdxGame(), cfg);
}
like image 196
P.T. Avatar answered Jun 07 '26 23:06

P.T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!