How to keep an Android display turned on while your app is running

If you want to make sure that your users’ Android display stays on while your app is running -- such as if you are writing a game/gaming app -- use this call and flag in the onCreate method of your Android Activity:

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

Here’s what the Android docs say about the FLAG_KEEP_SCREEN_ON setting:

“As long as this window is visible to the user, keep the device's screen turned on and bright.”

To be clear, you can set this in the onCreate method of an Activity, like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}