1

I am creating a game for Android using LibGDX. On one of my game modes the user uses the Accelerometer to move the player however, the screen will go dim and to sleep since the user is not touching the screen.

I know I can add and clear flags to keep the screen from going to sleep during the entire app:

// Add flags
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Clear flags
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).

Using that to keep the app awake works fine but I cant work out how to turn it on (and off) only for a specific screen (which uses the Accelerometer) because you must add the flag in the onCreate() method. Otherwise the screen will not go to sleep when the user is not on the game mode which uses the accelerometer (eg on the Main Menu...).


Basically, I want to be able to programmatically turn on and off the feature to prevent the screen from sleeping. Thanks for any answers!

Note: I don't really want to use a wake lock as it requires special permission and can potentially be dangerous (By not releasing it)


EDIT: I am using libGDX and the screen Interface for my various screens (eg menu, options, game...). As far as I'm aware this is all in only one android view. I want to be able to set "keep_Screen_on" to true in only one of my screens rather than the entire app.

Samuel T
  • 382
  • 1
  • 10
  • 1
    If you just want to prevent the sleep mode on a specific View, just call [link](http://developer.android.com/reference/android/view/View.html#setKeepScreenOn%28boolean%29)setKeepScreenOn(true) on that View or set the keepScreenOn property to true. This will prevent the screen from going off while the View is on the screen. No special permission required for this. Source : Witek's answer from https://stackoverflow.com/questions/3723634/how-do-i-prevent-an-android-device-from-going-to-sleep-programmatically – arjun May 24 '17 at 05:55
  • 1
    Possible duplicate of [Force Screen On](https://stackoverflow.com/questions/2131948/force-screen-on) – user3382203 May 24 '17 at 05:57
  • @MallikarjunM Thanks for replying. I am using LibGDX and the game/screen interface which means (I think aleast) the whole app is in one view. This means if I set the keepScreenOn property to true the entire app will have that property and not only for my specific game mode. – Samuel T May 24 '17 at 06:18
  • @Samuel May be you could set KEEP_SCREEN_ON using [setKeepScreenOn(true)](http://developer.android.com/reference/android/view/View.html#setKeepScreenOn%28boolean%29) for the specific game mode and reset the flag when you are in other modes – arjun May 24 '17 at 06:25

1 Answers1

1

You can use Interfacing for the same.

Create an interface inside your core module

public interface Service {

     void keepScreenOn(boolean isOn);
}

Implement this interface in android module

public class AndroidLauncher extends AndroidApplication implements Service {

   View gameView;

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

       gameView=initializeForView(new Main(this), config);
       setContentView(gameView);
   }

   @Override
   public void keepScreenOn(final boolean isOn) {

       runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
                gameView.setKeepScreenOn(isOn); 
            } 
       });
   }
 } 

Main/ApplicationListener class inside core module

public class Main extends ApplicationAdapter {

   Service service;

   public Main(Service service){
      this.service=service;
   }
}

You've reference of Service, When you move to particular screen then can call keepScreenOn() of Service interface.

I've not tested this but it should work.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • Thanks, I tried it and it worked. The only issue was that I needed to run the 'view.setKeepScreenOn(isOn)' on the ui thread by doing: runOnUiThread(new Runnable() { @Override public void run() { view.setKeepScreenOn(isOn); } }); – Samuel T May 24 '17 at 08:26