77

I used:

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

How do I resume to Default state (no-keep-on)?

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
OkyDokyman
  • 3,786
  • 7
  • 38
  • 50
  • I know answer is accepted but I wonder what would be your use case for this? do you want to keep screen on while activity is on screen, after it or what? When would you like to set it back to "default (no-keep-on) state"? Thanks for clarifications – Ewoks Jun 19 '13 at 13:21
  • 4
    @Ewoks - One use case is when you provide a user preference to keep the screen on during a particular activity (e.g., reading text) and the user turns it turns off while the activity is active (or on the back stack). You'd then want to turn off the flag without restarting the activity itself. (The alternative would be that the preference change does not take effect until the activity is finished and restarted.) – Ted Hopp Oct 20 '14 at 16:33

4 Answers4

157

I think this should do it:

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

See API for details.

Noel
  • 7,350
  • 1
  • 36
  • 26
  • 6
    Just a note for anyone who may not be aware. Adding and clearing these flags needs to be done on the UI thread. This caught me out in an odd way. Rather than just not working I saw intermittent and even reversed operation of the KEEP_SCREEN_ON flags. – Fat Monk Jan 27 '16 at 22:35
  • I was wonder why the answer received such a high rate but didn't work for me. After several hours, I really that there is another activity in my app keeps the screen on. So double check other activities if the above code doesn't work for you. :D – thanhbinh84 Sep 20 '17 at 07:18
  • I had no issue doing this in onCreate(). It probably doesn't matter, but I always set my flags before setContentView(). There might have been a reason why, but I've been using the same setup for so long I can't remember why. Also, some of these flags may stop working if your minSdkVersion is set to a higher value. If something isn't working, that may be why. – FoxDonut Nov 16 '20 at 07:43
20

If you instead set a flag android:keepScreenOn="true" (documentation) only on the views that need to keep the screen on, you wouldn't need to reset the flag manually.

Alexander Abramov
  • 1,470
  • 14
  • 20
4

Another approach

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

Also read this

and you can also set android:keepScreenOn="true" in the root View in xml.

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

Directly from documentation:

Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you no longer want the screen to stay on in your running application (for example, if you want the screen to time out after a certain period of inactivity). The window manager takes care of ensuring that the right things happen when the app goes into the background or returns to the foreground. But if you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags(): getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).