Force Screen On
Asked Answered
A

4

125

How do I force the screen to stay active and not shut off while my app is running?

Andros answered 25/1, 2010 at 11:42 Comment(0)
C
356

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.

Concord answered 25/1, 2010 at 18:33 Comment(16)
Does that prevent the device from sleeping? If so, the commonness of WAKE_LOCK strikes me as a shocking mistake!Yul
Yes it keeps the screen on and prevents the device from sleeping.Concord
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?Andros
Yes 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.Concord
@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 ????Halflight
why not just put the android:keepScreenOn="true" in your root view in your xml?Lateshalatest
That'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?Curium
@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.Concord
@Mike Gates have your AsyncTask send a message to the UI thread to have it modify the state of the flag as appropriate.Concord
Mobile 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.Indivertible
If there were ever such things, I would call this a hero post.Bezel
does 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.Steck
I found this as a very clean way of keeping screen onUnbalance
@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: https://mcmap.net/q/161111/-disable-keep-screen-onBatty
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.Boycott
and thus leave the screen on - and this is exactly what some people need, an why the API is there, doh! @Steck Just use the wake lock :)Helot
I
38

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 !

Iguana answered 28/8, 2013 at 11:37 Comment(1)
note v.setKeepScreenOn(true); seems to be the only way to do it for a fullscreen dialogfragmentFaro
L
19

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.

Laywoman answered 5/4, 2013 at 8:37 Comment(0)
B
0

There is a way to do it for the whole application via ActivityLifecycleCallbacks in your Application class.

  1. Create your custom Application class.

  2. Register it in the Manifest in the tag:

    <application android:name=".PlaygroundApp" .../>

  3. Implement ActivityLifecycleCallbacks and set keeping the screen on for every Activity.

package com.example.kotlinplayground

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.view.WindowManager

class PlaygroundApp : Application() {

    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                // Keep the screen on for each activity
                activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
            }

            override fun onActivityStarted(p0: Activity) {
            }

            override fun onActivityResumed(p0: Activity) {
            }

            override fun onActivityPaused(p0: Activity) {
            }

            override fun onActivityStopped(p0: Activity) {
            }

            override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
            }

            override fun onActivityDestroyed(p0: Activity) {
            }
        })
    }
}
Bukharin answered 18/12, 2023 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.