130

Is there a way to disable the screensaver while my app is running?

The dimming of the screen is what I mean.

Flexo
  • 87,323
  • 22
  • 191
  • 272
clamp
  • 33,000
  • 75
  • 203
  • 299
  • 3
    What about using `View.setKeepScreenOn()`[link][1]?. I've never used it myself though. [1]: http://developer.android.com/reference/android/view/View.html#setKeepScreenOn%28boolean%29 – dorzey Nov 16 '10 at 15:30
  • 3
    possible duplicate of [Force Screen On](http://stackoverflow.com/questions/2131948/force-screen-on) – Richard Ev Aug 29 '11 at 17:40
  • http://stackoverflow.com/questions/5712849/how-do-i-keep-the-screen-on-in-my-app/24261889#24261889 – Zar E Ahmer Jun 17 '14 at 11:04

12 Answers12

186

You want to use something like this:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Ian G. Clifton
  • 9,349
  • 2
  • 33
  • 34
110

I used:

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

to disable the screen timeout and

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

to re-enable it.

capellone78
  • 1,201
  • 1
  • 8
  • 2
97

There is also a XML way that Google recommends:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true">

Check Google Slides - Slide 16.

"Wakelocks are costly if forgotten (...) Consider using android:keepScreenOn to ensure correctness"

neteinstein
  • 17,529
  • 11
  • 93
  • 123
2

In a View, in my case a SurfaceView subclass, you can set the screen on always on. I wanted the screen to stay on while this view was still drawing stuff.

public class MyCoolSurfaceView extends SurfaceView { 
@Override
protected void onAttachedToWindow (){
    super.onAttachedToWindow();
    this.setKeepScreenOn(true);
}

@Override
protected void onDetachedFromWindow(){
    super.onDetachedFromWindow();
    this.setKeepScreenOn(false);
}
orkoden
  • 18,946
  • 4
  • 59
  • 50
  • 1
    That is actually more complicated than needed. Just call setKeepsScreenOn(true) anywhere, and the framework will automatically take care of keeping the screen on when your window is being shown. – hackbod Sep 24 '12 at 07:58
2

Its importante to note that these methods all must be run from the UI thread to work. See changing KeepScreenOn from javascript in Android cordova app

Community
  • 1
  • 1
2

Simple add below line into your MainActivity and your App never turn lights off.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Behzad
  • 3,502
  • 4
  • 36
  • 63
2

1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);is best solution for Native Android.
2. if you want to do with React android application then please use the below code.

@ReactMethod
    public void activate() {
        final Activity activity = getCurrentActivity();
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                }
            });
        }

    }
Manisha
  • 231
  • 1
  • 3
  • 12
1

Put this at onStart

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "no sleep");
    wakeLock.acquire();

And this at you manifest

    <uses-permission android:name="android.permission.WAKE_LOCK" />

Don't forget to

wakeLock.release();

at onStop

Gonkas
  • 93
  • 2
  • 11
0

this is the best way to solve this

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
MBMJ
  • 5,323
  • 8
  • 32
  • 51
0

This can be done by acquiring a Wake Lock.

I didn't tested it myself, but here is a small tutorial on this.

wonne
  • 108
  • 1
  • 8
  • 11
    No no no. Wake locks are easily misused and can drain the user's battery if not used very carefully. Use `View#setKeepScreenOn` or its xml layout attribute equivalent, `android:keepScreenOn="true"` – adamp Nov 16 '10 at 16:22
  • 5
    @adamp: The OP did not indicate whether his app is an activity or a service. An activity should use the mechanism you cite. A service should use a `WakeLock`. – CommonsWare Nov 16 '10 at 16:33
  • thanks its not a service. its similar to a picture viewer and i just dont want the screen to be dimmed. – clamp Nov 16 '10 at 16:37
  • @adamp: your mention of `android:keepScreenOn="true"` is the only valid answer for activities in my opinion. Stay away from wakelocks unless absolutely necessary. – pauluss86 Sep 21 '12 at 10:31
  • Not just Activities but any sort of UI that shows a window. The accepted answer of using the corresponding window flag is equally valid. – adamp Sep 22 '12 at 00:33
  • Simply setting the keeoScreenOn flag ony prevents the screen from turning off - it doesn't prevent the screen from dimming! – slott Nov 17 '15 at 12:31
  • This website is no longer accessible. Any answers are best put into the content of your post (even if not your content as you can attribute it to the author) – brianlmerritt Jan 26 '19 at 09:41
  • The posted link in this answer now leads to a porn site. – Aaron Hayman Oct 31 '19 at 19:14
0
android:keepScreenOn="true"

is the better way to handle this if we want to make any particular screen awake all the time otherwise we have to handle yourself when to enable it and to disable it

https://developer.android.com/training/scheduling/wakelock.html

Sandeep Tengale
  • 152
  • 4
  • 13
0
procedure SetSleep(aEnable:Boolean);
var
    vFlags: integer;
begin
    vFlags := TJWindowManager_LayoutParams.JavaClass.FLAG_TURN_SCREEN_ON or
        TJWindowManager_LayoutParams.JavaClass.FLAG_DISMISS_KEYGUARD or
        TJWindowManager_LayoutParams.JavaClass.FLAG_SHOW_WHEN_LOCKED or
        TJWindowManager_LayoutParams.JavaClass.FLAG_KEEP_SCREEN_ON;

    if aEnable then
    begin
      CallInUIThread (   // uses FMX.Helpers.Android
      procedure
      begin
        TAndroidHelper.Activity.getWindow.setFlags (vFlags, vFlags);
      end );
    end
    else
      CallInUIThread (
      procedure
      begin
        TAndroidHelper.Activity.getWindow.clearFlags (vFlags);
      end );
end;