Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: prevent display from turning off

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?

regards

like image 318
Simon Lenz Avatar asked Apr 15 '12 19:04

Simon Lenz


4 Answers

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

like image 137
waqaslam Avatar answered Oct 05 '22 13:10

waqaslam


use WakeLock

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

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()

like image 37
confucius Avatar answered Oct 05 '22 12:10

confucius


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

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

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

Best wishes, Tim

like image 33
Tim Avatar answered Oct 05 '22 14:10

Tim


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

like image 41
mvdnes Avatar answered Oct 05 '22 14:10

mvdnes