Keep screen on in Activity - does not work with FLAG_KEEP_SCREEN_ON
Asked Answered
E

2

10

For a long time I thought that I knew how to stop the screen from going into sleep mode, I simply used this code in my Activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

However, I realized that this only worked when my phone was in "developer mode", ie when the USB debugging (Settings --> Developer options --> USB debugging) was enabled/checked. Then the above code indeed stops the screen/device to go to sleep.

When that debugging is not checked, then my screen goes to sleep like there's no tomorrow. I am running Android 4.04 on my device, and

 android:minSdkVersion="12"
 android:targetSdkVersion="16"

Edit

I have tested with Commonsware's suggestion, and added the setKeepScreenOn() to the code, so it looks like this:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
View root = findViewById(android.R.id.content); 
if (root != null)
    root.setKeepScreenOn(true);

I have also checked so that this code is actually executed, and it is. But it doesn't change a thing.

Embonpoint answered 3/3, 2013 at 19:22 Comment(12)
Ill try it, but it seems that the FLAG_KEEP_SCREEN_ON might even work if the app is "deployed" via Eclipse, but not if I download the exact same code from Google Play. And yet, everyone "out there" on the net says that FLAG_KEEP_SCREEN_ON works... I cant be the only one with this issue?Embonpoint
Since I have never used FLAG_KEEP_SCREEN_ON, and only have ever used setKeepScreenOn() (as it is simpler), I cannot comment regarding the efficacy of your FLAG_KEEP_SCREEN_ON code.Archaize
Ok, it doesnt work with the setKeepScreenOn() either.Embonpoint
Since this stuff works for lots of other developers on lots of other apps, I'd say the issue is with your device.Archaize
Its a Samsung S2, android version 4.0.4. Sounds too mainstream to not work... The same issue is for Samsung S3 Mini, that I bought 1 week ago.Embonpoint
github.com/commonsguy/cw-omnibus/tree/master/MiscUI/… That sample project demonstrates the use of setKeepScreenOn() for a "delayed timeout", where you want to keep the screen lit for a while, but not indefinitely if the user is not actively using your app. This sample has worked successfully on all hardware that I have tried it on.Archaize
Thanks for that CommonsWare. I use the same code/methods that you use, and cannot really see why it shouldnt work in my case. I will do some debug-printouts, as there seem to be some form of difference if there is a debugger connected, if the phone is in "developer mode" etc...Embonpoint
ted, do you have the same problem with setKeepScreenOn() on a View?Orchard
I tried CommonsWare's suggestion and it works perfectly, (I just used setKeepScreenOn() on android.R.id.content, without the FLAG_KEEP_SCREEN_ON flag). I also tried it without the developer mode nor the run feature from Eclipse (I copied the APK file to the device and run the installer) and it works smoothly.Kwon
So, a long time later, I think the screen is actually kept on normally, but I have seen sometimes that it doesnt. But I havent looked at it further...Embonpoint
Is the window/view having this flag visible while the phone goes to sleep?Morph
I've been looking at this today and have found that I'm able to keep the screen on if I do it during OnCreate(). It works using either method (FLAG_KEEP_SCREEN_ON flag or View.setKeepScreenOn(true)). If I try to keep the screen on sometime after OnCreate() (based on user input, for example), it doesn't work.Dace
A
2

i was facing same problem, i was using one activity for my project and all the other screen are fragments then i used the android:keepScreenOn="true" in main activity.

please try to use this and let me know if you didn't get your desire result.

Thanks.

Assibilate answered 26/9, 2014 at 17:8 Comment(0)
O
1

Only solution which realy works in my app is WakeLock in main application class. Unfortunately the SCREEN_BRIGHT_WAKE_LOCK flag is deprecated!

public class MyApp extends Application {
    PowerManager.WakeLock screenOnWakeLock;
    @Override
    public void onCreate() {
        super.onCreate();
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock =  powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,"ScreenlockTag");
        wakeLock.acquire();
    }

    @Override
    public void onTerminate()
    {
        if (screenOnWakeLock != null && screenOnWakeLock.isHeld())
            screenOnWakeLock.release();
        super.onTerminate();
    }
}
Ovotestis answered 11/9, 2015 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.