9

So, 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 codes 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"

Anyone heard about this issue?

EDIT

I have tested with Commonswares 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 doesnt change a thing...

Ted
  • 18,194
  • 33
  • 89
  • 142
  • 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? – Ted Mar 03 '13 at 21:46
  • 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. – CommonsWare Mar 03 '13 at 21:48
  • Ok, it doesnt work with the setKeepScreenOn() either. – Ted Mar 06 '13 at 09:05
  • 1
    Since this stuff works for lots of other developers on lots of other apps, I'd say the issue is with your device. – CommonsWare Mar 06 '13 at 12:13
  • 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. – Ted Mar 07 '13 at 11:12
  • 1
    https://github.com/commonsguy/cw-omnibus/tree/master/MiscUI/DelayedTimeout 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. – CommonsWare Mar 07 '13 at 12:09
  • 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... – Ted Mar 07 '13 at 12:39
  • ted, do you have the same problem with setKeepScreenOn() on a View? – steveh Mar 31 '13 at 02:37
  • 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. – Leeeeeeelo Aug 26 '13 at 08:03
  • 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... – Ted Sep 02 '13 at 08:09
  • Is the window/view having this flag visible while the phone goes to sleep? – Louis CAD Jan 27 '16 at 10:13
  • 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. – mikejonesguy Jan 26 '17 at 05:10

2 Answers2

1

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.

Aman
  • 109
  • 8
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();
    }
}
Kamil Svoboda
  • 1,383
  • 1
  • 9
  • 7