Reputation: 31
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
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
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
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