Search code examples
libgdx

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.


Solution

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