How do I force the screen to stay active and not shut off while my app is running?
3 Answers
PLEASE DO NOT USE A WAKE LOCK
This requires that you give your app an additional permission, and it is very easy to introduce bugs where you accidentally remain holding the wake lock and thus leave the screen on.
It is far, far better to use the window flag FLAG_KEEP_SCREEN_ON
, which you can enable on your activity's window in your onCreate()
like this:
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
This will make sure that the screen stays on while your window is in the foreground, and only while it is in the foreground. It greatly simplifies this common use case, eliminating any juggling you need to do as your app transitions between states.

- 90,959
- 16
- 217
- 251

- 90,665
- 16
- 140
- 154
-
5Does that prevent the device from sleeping? If so, the commonness of WAKE_LOCK strikes me as a shocking mistake! – Michael Cramer Jan 25 '10 at 18:52
-
32Yes it keeps the screen on and prevents the device from sleeping. – hackbod Jan 26 '10 at 00:55
-
I am currently using a Wake Lock to make sure the device does not turn off while tracking with the GPS. When the user stops tracking I release the wake lock. If I use your suggestion above, is it possible to remove the flag after you add it. Would I need to redraw the window? – bugzy Feb 08 '10 at 20:04
-
1Yes you can remove the flag, with the appropriate window API. You don't need to worry about causing anything to be drawn, the framework does that if needed. – hackbod Feb 09 '10 at 03:26
-
1@hakbod: this would work for kepping the screen alive , but what about screen which is asleep , would it wake it up and keep it alive ???? – Kavitha Jul 22 '11 at 19:15
-
why not just put the android:keepScreenOn="true" in your root view in your xml? – Dhruv Aug 02 '11 at 07:23
-
1That's great, but I want my screen to stay bright only while an AsyncTask is running. How do I turn this option "off" after it is completed? – Mxyk Aug 25 '11 at 18:27
-
5@KC202 you can use WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON to cause the screen to turn on when your window is displayed. Also often used with FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED. – hackbod Aug 26 '11 at 08:05
-
@Mike Gates have your AsyncTask send a message to the UI thread to have it modify the state of the flag as appropriate. – hackbod Aug 26 '11 at 08:06
-
2Mobile n00b here doing some PhoneGap work: this answer worked for me, but only after I added the following two lines to my application's main Java file: `import android.view.WindowManager; import android.view.Window;` They weren't in there by default. Hope it helps someone. – Chris Allen Lane Nov 08 '12 at 23:16
-
If there were ever such things, I would call this a hero post. – Lo-Tan Jan 23 '13 at 23:24
-
1does anyone know if there is a way to do this across all activities within an application? i would prefer this code be in one place, not in each activity. – pearcewg Aug 03 '13 at 14:41
-
I found this as a very clean way of keeping screen on – Nitish Dec 14 '13 at 04:52
-
1@MikeGates, turning off is done in the following way: `getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)`. See the following for further info: http://stackoverflow.com/a/4807777/435605 – AlikElzin-kilaka Dec 29 '13 at 13:14
-
I use FLAG_KEEP_SCREEN_ON but I get intermittent 01-18 10:20:08.924: E/power(337): acquire_wake_lock g_error=2 01-18 10:20:10.926: E/power(337): release_wake_lock g_error=2 01-18 10:20:11.196: E/power(337): acquire_wake_lock g_error=2 01-18 10:20:13.208: E/power(337): release_wake_lock g_error=2 messages in log throughout app run. it does not appear to affect app but I would like to clean it up. should I use android.permission.WAKE_LOCK permission to clean it up? I don't want to ask for a permission I don't really need. – Androidcoder Jan 18 '14 at 16:57
-
`and thus leave the screen on` - and this is exactly what some people need, an why the API is there, doh! @pearcewg Just use the wake lock :) – kellogs Jan 19 '17 at 19:45
This Question has Already Great Answer by @hackbod !
I am Answering this Question with Two Additional Solutions !
Existing Solution :
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Additional Solutions:
we can use keepScreenOn
1. implementation
using setKeepScreenOn() in java code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// or any View (in case generated programmatically )
View v = getLayoutInflater().inflate(R.layout.driver_home, null);
v.setKeepScreenOn(true);
setContentView(v);
}
Docs http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
2. Adding keepScreenOn
to xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
Docs http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
Note ( Some Useful Points) :
1. it Doesn't matter that keepScreenOn
should be used on Main/Root/Parent View
it can be used with any child view
will work As same as it works in Parent view
2. The Thing Only matter is that View's Visibility must be visible
other wise it will not work !

- 72,056
- 11
- 123
- 141

- 14,139
- 7
- 51
- 71
-
note `v.setKeepScreenOn(true);` seems to be the only way to do it for a fullscreen dialogfragment – Tim Jan 23 '18 at 11:30
Another solution is to add android:keepScreenOn="true"
(documentation) to the views that need to keep the screen on.
Allows for a little bit more granular control in terms of which views stay on and which don't. You can even reference a setting from a resource file this way.

- 72,056
- 11
- 123
- 141

- 1,470
- 14
- 20