Kharak
Kharak

Reputation: 72

How to awake screen while game play in libgdx?

I am creating an game using accelerometer in libgdx and while playing the game, it sleeps after some, i need awake this untill game-play. i got this in some forums

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

which work for android app,but don't how to do that in libgdx, it is unrecognizable by compiler.any help will be appreciated.

Upvotes: 2

Views: 405

Answers (2)

AAryan
AAryan

Reputation: 20140

onCreate method of AndoridLauncher create a configuration object and set useWakelock=true

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

initialize(new MyGdxGame(), config);

Upvotes: 3

Kharak
Kharak

Reputation: 384

Best and easiest way to do that is

  1. Go to the android folder of your project in android studio

  2. then locate AndroidLauncher.java in src sub-folder

  3. then copy and paste code below

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

    after adding code whole file will look like this

    public class AndroidLauncher extends AndroidApplication { @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); initialize(new MyBomber(), config);

    }

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

Upvotes: 3

Related Questions