26

I am using the method setKeepScreenOn(true) and haven't been able to figure out how to call this in relation to the current Activity (which has a content view set). I've been able to get it to work by calling it on one of my buttons which is always present in the view, but this feels wrong - and I'm sure there must be a way to get around this. I tried referencing the current focus like this:

getCurrentFocus().setKeepScreenOn(true);

but that threw a NullPointerException. Maybe there was no current focus. So, can anyone tell me how I can reference the view class which I am working inside? Thanks :)

Matthew
  • 44,826
  • 10
  • 98
  • 87
Emma Assin
  • 851
  • 2
  • 12
  • 23
  • possible duplicate of [How to keep android device from sleeping while plugged in](http://stackoverflow.com/questions/3949917/how-to-keep-android-device-from-sleeping-while-plugged-in) – Matthew Mar 16 '11 at 20:15

5 Answers5

59

Try this answer:

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

getWindow is a method defined for activities, and won't require you to find a View first.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • nice, I was using wake locks before. – Tony D Jul 02 '12 at 19:12
  • This only works for me if the phone is in "developer mode", so if you go into settings --> "USB-something". Then it works to use the FLAG_KEEP_SCREEN_ON, but if I do not have the developer mode checked, then it has no effect at all. – Ted Mar 03 '13 at 19:15
  • 6
    @Ted: What you've written here and in (at least) two other questions is just wrong. We should put it right for others: `FLAG_KEEP_SCREEN_ON` works perfectly for what it describes: It just makes the screen keep on. Neither is there any permission required for this to work nor does it only work if your device is in debug mode. It should work on every device, and if it doesn't, it's the device's fault ;) – caw Oct 30 '13 at 07:41
40

As Hawk said but poorly explained.

You can also use FLAG_KEEP_SCREEN_ON in your XML layout file.

Note the android:keepScreenOn="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <!-- whatever is in your layout -->

</LinearLayout>

I've now written all the choices for keeping the screen on up into a blog post:
https://blog.blundellapps.co.uk/tut-keep-screen-onawake-3-possible-ways/

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
Blundell
  • 75,855
  • 30
  • 208
  • 233
5

Set android:keepScreenOn in XML

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
Hawk
  • 61
  • 1
  • 1
1

If you are doing it on a class extends View. You can simple:

this.setKeepScreenOn(true);
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • hmm. my class extends Activity, but as I setContentView I assumed it was also a View - maybe not the case? – Emma Assin Mar 16 '11 at 20:35
  • It's not a view, actually. But, setContentView does take a View parameter. I think he misread your question. – Matthew Mar 16 '11 at 20:51
0

According to Google Docs for android Developers you've two ways to do this :

First way :

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);
}

Second way is to add in your xml file layout this attribute: android:keepScreenOn="true"

josliber
  • 43,891
  • 12
  • 98
  • 133
Yacine MEDDAH
  • 1,211
  • 13
  • 17