46

I have an Activity that usually needs some time to watch the screen without interacting with it.

The problem is that the screen turns off, just like with almos any other app. I want to prevent that automatic turn off for my Activity, like Dolphin HD does (if you configure it from the settings menu).

I want to let the user to turn off the screen pressing the usual block button, but prevent the automatic block of the device while in my Activity.

How can I achieve this?

Thanx.

HandlerExploit
  • 8,131
  • 4
  • 31
  • 50
Alex
  • 1,449
  • 4
  • 18
  • 28

6 Answers6

105

Add android:keepScreenOn="true" to some widget in your layout XML resource for this activity. So long as that widget is visible on the screen, the screen will not turn off automatically.

EDIT:

A WakeLock, as suggested by other answers, technically will work. But then you have to manually release the WakeLock (if you mess that up, the screen will stay on a long time). And, since you could mess it up, you need the WAKE_LOCK permission. Using keepScreenOn avoids all of that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • You mean widget in the sense "widget component"? Or some view? – Gangnus Feb 17 '12 at 22:00
  • Thanx for the answer, that worked pretty well :) I think the wakelock may be a good way, but this one is easier. – Alex Feb 17 '12 at 22:29
  • 1
    I tried to add this attribute to a view that's inside of a fragment that is initially GONE and gets set to VISIBLE by code in the app. This did not work: the screen still switches off. The answer by "noelicus" works well. – treesAreEverywhere May 12 '14 at 14:33
  • @treesAreEverywhere: `android:keepScreenOn` is for simpler scenarios. – CommonsWare May 12 '14 at 14:39
  • 2
    It won't work if you change the visibility of the view that has the "keepScreenOn" attribute. E.g. if you have the view with the attribute initially hidden and show it later, the screen will still turn off. In those cases, use WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON as described in "noelicus"'s answer. – treesAreEverywhere May 12 '14 at 14:55
  • I have set `keepScreenOn=true` within a **Fragment** hosted within a **DialogFragment** and it works as expected initially. However, when causing a configuration change with a screen rotation the display is not staying on. – AdamHurwitz Dec 24 '18 at 03:03
  • @VolodymyrBuberenko: `android:keepScreenOn="true"` goes in a layout resource, as is noted in [the documentation that you linked to in your issue](https://developer.android.com/training/scheduling/wakelock#screen) and in my answer. AFAIK, that attribute was never supported in the manifest. – CommonsWare Dec 11 '19 at 12:14
  • @CommonsWare Wow, thanks. My mistake. Somehow was confused with current project. Going to remove previous comment to not confuse other people. – Volodymyr Buberenko Dec 14 '19 at 10:54
39

To change it on-the-fly do this:

if (keepScreenOn)
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
else
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
noelicus
  • 14,468
  • 3
  • 92
  • 111
4

Handling Fragments and Screen Rotation

The Android documentation, Keep the device awake outlines each solution.

Solution #1 - Flags

Documentation - Alternatives to using wake locks

For Fragments use the programmatic approach that is less battery intensive.

Enable

activity!!.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Disable

activity!!.window.clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON)

For my use case I called this in onStop() so the screen would default to the normal configuration when the Fragment showing the media content is exited.

Avoid

keepScreenOn=true does not work when there is a configuration change such as a screen rotation for Fragments.

Note: Android's Keep the device awake documentation should be updated accordingly to handle this case.

Solution #2 - Wake Lock

Documentation - Keep the CPU on with Wake Locks

A Wake Lock offers more control over keeping the specific elements of the device awake, but is more battery intensive, and important to release manually to save the battery since it is not handled automatically by the system.

AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
2

For "Xamarin Android":

Window.AddFlags(WindowManagerFlags.KeepScreenOn);
Matheus Miranda
  • 1,755
  • 2
  • 21
  • 36
1

You'll want to add WAKE_LOCK to your manifest, and set and remove it as needed within your app. See the google docs here for PowerManager.WAKE_LOCK

http://developer.android.com/reference/android/os/PowerManager.html

Plastic Sturgeon
  • 12,527
  • 4
  • 33
  • 47
1

You may want to use the wake-lock to prevent the screen off.Pleas refer http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Bhavya
  • 161
  • 4
  • Thanx for the answer and for the link to the doc, its always nice to get to understand how to do this kind of things, but this time i think i will go with the @CommonsWare solution :P – Alex Feb 17 '12 at 22:31