I don't want to use a WakeLock.
Is there a simple way to limit the time the screen is on using FLAG_KEEP_SCREEN_ON

- 14,703
- 8
- 66
- 110

- 809
- 13
- 25
-
You could use `Hander.postDelayed(...)` (or another delayed-execution option), and use the solution [here](http://stackoverflow.com/questions/4807634/disable-keep-screen-on) to disable the keep_screen_on. You could use it in conjunction with [this solution](http://stackoverflow.com/questions/6756768/turn-off-screen-on-android/6757206#6757206) to actually shut the screen off – Reed Feb 20 '15 at 19:09
1 Answers
If you read the docs here : docs
than you see that you don't need to take care about this. But you can, see:
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).
You could use this in conjunction with a Runnable, post delayed on the Handler, which is the Android way to go, or with a TimerTask, which would be the more Java way.
Example:
final long FIVE_MINUTES = 1000*60*5;
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
};
handler.postDelayed(r, FIVE_MINUTES);
Hope it helps.

- 3,336
- 24
- 55