13

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!

Johan
  • 427
  • 2
  • 7
  • 14

2 Answers2

29

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.

miva2
  • 2,111
  • 25
  • 34
Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • Hmm on the second "getWindow" eclipse tells me that "getWindow cannot be resolved or is not a field" – Johan Jan 07 '11 at 00:24
  • I meant getWindow(). But I'm not entirely certain the getFlags method works like that. I didn't actually pull up the api – Falmarri Jan 07 '11 at 00:25
  • 1
    'this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);' works. Is there an easy way to check if the flag is active or not (if the app is in fullscreen mode)? – Johan Jan 07 '11 at 01:42
  • 1
    There is no getFlags() function in the Window class. – Yar Oct 18 '11 at 13:42
  • 3
    'this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);' Worked great for me as well. Thank you! – MinceMan May 12 '12 at 20:10
  • 1
    There is no `getFlags()` function, but you you can get them by calling `getAttributes().flags` – Jon May 15 '13 at 19:53
  • Thanks Jon! It worked well for me. Falmarri you might want to edit your answer as the getFlags() part is actually wrong. – pcans May 24 '13 at 08:18
2

Try this

getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Also read this

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300