-1

What code do I need to add to a certain activity in my app to get it to override the sleep setting of the user's phone. I want the screen to not be able to shut off during a certain activity.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
jizco Borneo
  • 133
  • 1
  • 2
  • 12
  • See http://stackoverflow.com/questions/3723634/how-do-i-prevent-an-android-device-from-going-to-sleep-programmatically – Jonik Jul 15 '14 at 08:13

3 Answers3

0
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

to force screen always on, and

getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

to go back to default behavior.

type-a1pha
  • 1,891
  • 13
  • 19
0

If by "a certain activity", you mean "while a certain Activity is in the foreground", the easiest way is to add android:keepScreenOn="true" to some widget in the layout of that activity. So long as that widget is visible, the screen will not turn off. This works well for things like video players. The FLAG_KEEP_SCREEN_ON approach in the other answer contributed so far does the same thing, with more typing. :-)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

I suggest you use WakeLock.

Like this:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK,
            "Screenlock active");

Then use:

wl.acquire();
wl.release();
LordMarty
  • 1,551
  • 1
  • 12
  • 17