Android: WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON question

Johan picture Johan · Jan 7, 2011 · Viewed 26k times · Source

Im using the following code to keep the screen on:

this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Is there any way to disable/remove the FLAG_KEEP_SCREEN_ON later in the code? (I want the screen to fadeout normally).

Thanks!

Answer

Falmarri picture Falmarri · Jan 7, 2011

You could probably do something like this

this.getWindow().setFlags(this.getWindow().getFlags() & ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Did you look at the API? There's also this method

http://developer.android.com/reference/android/view/Window.html#clearFlags%28int%29

this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

I have not tried this either yet.

I imagine this will work to check if the flag is set:

this.getWindow().getFlags() & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

Edit: As per the comments, apparently this is how you get the value of the flag.

this.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

There might be a method for that too, you should look at the API doc.