Home » Android » Android: prevent display from turning off

Android: prevent display from turning off

Posted by: admin June 15, 2020 Leave a comment


I’ve written a small game, that is only controlled via some sensors. There is no touchscreninput or something similar.

The problem is, that after a few seconds of gaming, the screen turns off (because of no touch-input)

Is there something like a manifest-entry that prevents the screen from this behaviour?


How to&Answers:

The simplest would be adding android:keepScreenOn="true" to the layout in your xml.


use this code in the your game’s Activity as first line in the onCreate() after the super call:


This will result in the System handling the screen for you.

Best wishes,


use WakeLock

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag");

and put the permission in your manifest file

<uses-permission android:name="android.permission.WAKE_LOCK" />

dont forget to release that lock in your onStop()


You can request a WakeLock as specified here: http://developer.android.com/reference/android/os/PowerManager.html