11

I have a game that uses the accelerometer to move a guy back and forth on the screen. After a few seconds the screen goes dim. Is there any way to keep it alive and bright during the game activity?

Robin Rodricks
  • 2,751
  • 3
  • 28
  • 52

3 Answers3

20

With the PowerManager API, you can acquire and release locks to:

  • prevent your device to pause;
  • prevent your device to dim;
  • force brightness
  • ...

Warning you must add one permission:

<uses-permission android:name="android.permission.WAKE_LOCK" />
Ellis
  • 3,626
  • 19
  • 28
3

I try like that in oncreate on activity start

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Kalpesh
  • 151
  • 1
  • 4
0

This blog post covers the three ways of achieving this:

http://blog.blundell-apps.com/tut-keep-screen-onawake-3-possible-ways/

Basically, you either:

  • tell android through your layout xml that you want to keep the screen alive
  • tell android through a call to window manager to keep the screen on during oncreate
  • use wakelocks to force the device to use full power at all time (and drain batteries)
Jari Komppa
  • 7,873
  • 3
  • 30
  • 62