Disable keep screen on
Asked Answered
L

5

78

I used:

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

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

Latarsha answered 26/1, 2011 at 17:18 Comment(2)
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 clarificationsUncommitted
@Uncommitted - 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.)Youngran
R
159

I think this should do it:

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

See API for details.

Revisionist answered 26/1, 2011 at 17:30 Comment(3)
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.Tantalous
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. :DLungworm
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.Greek
N
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.

Nasion answered 5/4, 2013 at 8:42 Comment(0)
F
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.

Ferbam answered 16/6, 2015 at 13:16 Comment(0)
G
1

I've created these kotlin extensions:

fun Activity.keepScreenOn(rootView: View? = null) {
    window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    rootView?.keepScreenOn = true
}
fun Activity.clearScreenOn() {
    window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}

Then onCreate you can call keepScreenOn() and onDestroy clearScreenOn()

Gluten answered 24/11, 2023 at 17:21 Comment(0)
L
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).

Lunula answered 8/11, 2018 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.