user2141514
user2141514

Reputation: 31

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.

Upvotes: 3

Views: 2782

Answers (3)

Kharak
Kharak

Reputation: 384

Better and easier way,no need to worry about releasing anythinng

Just copy this code in AndroidLauncher.java file

@Override protected void createWakeLock(boolean use) { use=true; super.createWakeLock(use); }

for more seeenter link description here

Upvotes: 0

P.T.
P.T.

Reputation: 25177

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);
}

Upvotes: 8

JRowan
JRowan

Reputation: 7104

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
wakeLock.acquire();

and you have to put the permission wakelock in your manifest

and when your done with the wakeLock release it

wakeLock.release();

or

A safer approach is to use setKeepScreenOn() or android:keepScreenOn for some View in the activity. You do not need the WAKE_LOCK permission, and you cannot readily screw it up by failing to release it. As soon as the keep-screen-on View is no longer in the foreground, device behavior returns to normal.

Upvotes: 1

Related Questions