I have three activities in my app. I want to keep the screen awake when it is in the second activity. The screen should not go off in my second activity unless the "lock" key is pressed manually. I went through many links but they seem unclear to me.
-
See detail answer here... http://stackoverflow.com/questions/5712849/how-do-i-keep-the-screen-on-in-my-app/24261889#24261889 – Zar E Ahmer Jun 17 '14 at 11:03
5 Answers
As discussed in the Android tutorial Keep the Screen On, you can do this in a few ways. You can set the FLAG_KEEP_SCREEN_ON on the activity's window:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
An XML equivalent for that is to add the attribute android:keepScreenOn="true"
to the root view of your activity's layout. The advantage of setting the flag programmatically is that you can use
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
when you no longer need to force the screen to stay on while your activity is running.
Another way to control the screen (and certain other resources) is to use a wake lock:
mWakeLock = ((PowerManager) getContext().getSystemService(Context.POWER_SERVICE))
.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
mWakeLock.acquire();
// screen stays on in this section
mWakeLock.release();
The manifest will have to include this permission:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
However, as discussed in the tutorial, a wake lock is more appropriate for other use cases (such as a service or background task needing the CPU to keep running while the screen is off).

- 232,168
- 48
- 399
- 521
-
7The first choice is **much** better... don't use a wake lock!! It requires an additional permission in the manifest! http://stackoverflow.com/a/2134602/844882 – Alex Lockwood Dec 02 '12 at 16:14
-
11@AlexLockwood - For OP's application, the first approach is better. However, it is wrong to adopt a policy of "don't use a wake lock!" A wake lock provides more control over screen-on status. When the activity only requires the screen to be kept on during short periods, the wake lock can be released, saving battery life. With the first approach, the screen is kept on for the entire time the activity is in the foreground. Also, the first approach cannot be used by a Service performing work on behalf of an activity. – Ted Hopp Dec 02 '12 at 16:44
-
Yeah, sorry... there are situations where it is correct to use a wake lock. But if you simply want to keep the screen on for the duration of an activity's life cycle, the first choice is better. – Alex Lockwood Dec 02 '12 at 17:26
-
Simply add android:keepScreenOn="true" in the root layout of your activity Don't use wake lock. see here..http://stackoverflow.com/questions/2131948/force-screen-on/2134602#2134602 – Zar E Ahmer Jun 17 '14 at 11:02
-
1Shouldn't getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); be placed BEFORE setContentView()? – Yar Sep 14 '15 at 10:01
-
1@Yar - It doesn't matter. Although some flags indeed must be set before setting the content view, but `FLAG_KEEP_SCREEN_ON` isn't one of them. Even the [Google guide on keeping the screen on](https://developer.android.com/training/scheduling/wakelock.html) shows setting this flag after calling `setContentView()`, and points out that you can set and clear it at will during the lifetime of your activity. – Ted Hopp Sep 16 '15 at 02:29
I find this solution much easier:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" // Whatever your layout is
...
android:keepScreenOn="true"> // Add this line
Just add this to your activity layout XML.

- 18,373
- 5
- 39
- 42
As per I understand your question, I think you have to use WAKE_LOCK
for it in your application.
Something like,
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,"");
mWakeLock.acquire();
And in your application's manifest.xml file file add this,
<uses-permission android:name="android.permission.WAKE_LOCK" />

- 108,599
- 23
- 164
- 151
-
2[**Plz not use wake lock**](https://stackoverflow.com/questions/2131948/force-screen-on/2134602#2134602) – Aashish Kumar Apr 20 '20 at 10:36
try to use this
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

- 1,940
- 1
- 20
- 29
-
Can you add a little bit more clarification as to how your solution solves the problem? It will help the OP to better understand your solution. – KLee1 Nov 14 '12 at 18:19
This code is deprecated, use this instead:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
After you finish with usage, call (best solution is to call this method in onDestroy method of some activity):
wl.release();
More about this on this link

- 79
- 1
- 8
-
3[PowerManager.SCREEN_DIM_WAKE_LOCK](http://developer.android.com/reference/android/os/PowerManager.html#SCREEN_DIM_WAKE_LOCK) was deprecated in favor of [WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON](http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON) in API Level 17. – anon Nov 26 '13 at 20:00