I had the WAKE_LOCK permission in my app which I subsequently removed. But the phone still does not turn off the screen when my app is active. Is there any other setting that I might have that's preventing the phone from turning off?
-
1If you're down-voting the question, please provide a comment stating the reason. Otherwise, I'm none the wiser. – Rajath Sep 28 '15 at 06:50
-
"Is there any other setting that I might have that's preventing the phone from turning off". After your comment & looking at the down votes what my conclusion is: you left people guessing what you may have used instead of showing what things you have used that might cause the issue. – Syed Nazar Muhammad Sep 28 '15 at 06:56
-
@SyedNazarMuhammad, if someone is expecting me to share 3000 lines of code, they're being silly, because I had no idea where in the code the effect was. – Rajath Sep 28 '15 at 06:58
-
The other conclusion I can draw is that the term "WAKE_LOCK" somehow triggers some people to down-vote. If that is the case, all I can say is I did not put it there in first place :-) – Rajath Sep 28 '15 at 06:59
-
Some Flags are crystal clear according to their name as of this FLAG_KEEP_SCREEN_ON, you missed it but its very clear & i hope you wrote the code by yourself – Syed Nazar Muhammad Sep 28 '15 at 07:01
-
@SyedNazarMuhammad, nope - I had not written that part of the code – Rajath Sep 28 '15 at 07:02
2 Answers
There are a couple 'settings' that can prevent the screen from going to sleep, according to the docs:
Certain apps need to keep the screen turned on, such as games or movie apps. The best way to do this is to use the FLAG_KEEP_SCREEN_ON in your activity (and only in an activity, never in a service or other app component). For example:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }
The advantage of this approach is that unlike wake locks (discussed in Keep the CPU On), it doesn't require special permission, and the platform correctly manages the user moving between applications, without your app needing to worry about releasing unused resources.
Another way to implement this is in your application's layout XML file, by using the android:keepScreenOn attribute:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true"> ... </RelativeLayout>
Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON. You can use whichever approach is best for your app. The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off.
Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you no longer want the screen to stay on in your running application (for example, if you want the screen to time out after a certain period of inactivity). The window manager takes care of ensuring that the right things happen when the app goes into the background or returns to the foreground. But if you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags():
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).
Wakelock is another, but since you already know about that, I don't think I need to mention that here.

- 41,901
- 18
- 127
- 145
-
That's perfect - Thank you. FLAG_KEEP_SCREEN_ON was being used in the code. – Rajath Sep 28 '15 at 06:46
-
see this is why you got the Down Votes, you should have mentioned in the question that I have used this Flag, might this be the issue ? – Syed Nazar Muhammad Sep 28 '15 at 06:58
-
@SyedNazarMuhammad he didn't know the issue was there, so you can't expect him to share that specific piece of code – Tim Sep 28 '15 at 07:00
-
@SyedNazarMuhammad, if I had known that the app was using this flag, then why would I post the question in the first place? – Rajath Sep 28 '15 at 07:00
-
1
-
To avoid draining the battery, an Android device that is left idle quickly falls asleep. However, there are times when an application needs to wake up the screen or the CPU and keep it awake to complete some work.
Use:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
This approach is much more efficient than wakelock and doesnt require any permission
and to clear flags:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).

- 884
- 1
- 11
- 33