# Prevent sleep in android

**URL:** <https://discourse.processing.org/t/prevent-sleep-in-android/15011>\
**Category:** Processing for Android\
**Created:** [October 29, 2019, 11:55am UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011 "2019-10-29T11:55:49Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_Emmerson](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@J\_Emmerson](https://discourse.processing.org/u/J_Emmerson)\
**Post date:** [October 29, 2019, 11:55am UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/1 "2019-10-29T11:55:49Z")

</div>

I’m making a counter for a card game and I need a function to prevent android from entering sleep mode

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 29, 2019, 12:07pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/2 "2019-10-29T12:07:06Z")

</div>

Hi. I think you need WakeLock ([reference](https://developer.android.com/reference/android/os/PowerManager.html))

And also set it as a permission.

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [October 29, 2019, 12:12pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/3 "2019-10-29T12:12:27Z")

</div>

Well… is in Spanish, but maybe can help you:

> [@Evitar la atenuación del brillo al cabo de un rato - AS](https://discourse.processing.org/t/evitar-la-atenuacion-del-brillo-al-cabo-de-un-rato-as/13796/2):
>
> Parece ser que ya lo he conseguido, [aquí](https://stackoverflow.com/questions/18468377/how-to-keep-screen-brightness-on-android-all-the-time-while-application-runs) dan dos soluciones, la primera al menos a mí, no me ha funcionado, pero la segunda si: Poner esto en onResume() getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG\_KEEP\_SCREEN\_ON); Y esto en onPause() getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG\_KEEP\_SCREEN\_ON); Es más, estaba probando un poco y resulta que basta con meter sólo la línea de código en onResume() y onPause(), (he quitado el wakelock y no se suspende …

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 29, 2019, 12:23pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/4 "2019-10-29T12:23:35Z")

</div>

In this case you don’t need permission

```auto

import android.app.Activity;
import android.view.WindowManager;
Activity act;

void onStart(){
  act = this.getActivity();
  act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

```

---

<div class="post-metadata">

**Author:** ![J\_Emmerson](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@J\_Emmerson](https://discourse.processing.org/u/J_Emmerson)\
**Post date:** [October 29, 2019, 12:36pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/6 "2019-10-29T12:36:41Z")

</div>

I tried this code and my sketch didn’t work

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 29, 2019, 12:50pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/7 "2019-10-29T12:50:10Z")

</div>

I tried it now on 4.4 and 5.1 without problem.  
What error did it give?

---

<div class="post-metadata">

**Author:** ![J\_Emmerson](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@J\_Emmerson](https://discourse.processing.org/u/J_Emmerson)\
**Post date:** [October 29, 2019, 1:22pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/8 "2019-10-29T13:22:07Z")

</div>

I’d didn’t give an error, it just didn’t work, the sketch just stopped. And what is 4.4 and 5.1

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 29, 2019, 1:45pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/9 "2019-10-29T13:45:40Z")

</div>

> [@J\_Emmerson](#):
>
> And what is 4.4 and 5.1

Kitkat and Lollipop.  
Can you try it including this code within a basic sample sketch?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 29, 2019, 5:33pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/10 "2019-10-29T17:33:13Z")

</div>

@akenaton Maybe you can help here.  
If I use it in onCreate () and use the back key it goes into the background and when I call it back it runs well over and over. However when I use the home key, and recall, the app crashes.  
When I use the code in onRestart(), it’s de other way around.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 30, 2019, 10:21am UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/11 "2019-10-30T10:21:07Z")

</div>

@noel ===

as for me i use this code ( i have added some methods which are useless, except for verifying what happens)

```auto

import android.os.Bundle;
import android.view.WindowManager;
boolean keep = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   
    KeepScreen();
   
};

@Override
public void onStart() {
    super.onStart();
    // this is called only when you have pressed the home button and restarted the app
     if(!keep){
    KeepScreen();
    
    }
    
}
@Override
public void onPause() {
    super.onPause();
   
}

@Override
public void onResume() {
    super.onResume();
   
     if(!keep){
        
    KeepScreen();
    }
}

@Override
public void onStop() {
    super.onStop();
    
    //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    // clearFlags will work but it is useless
    keep=false;
    
}

@Override
public void onDestroy() {
    super.onDestroy();
   
}

void setup(){
  
  size(800,600);
  orientation(PORTRAIT);
  background(255,0,0);
 
}
void draw(){
  background(0,0,255);
  textSize(24);
  if(keep ){
    text("ecran keep", width/2,height/2);
  }
};

private void KeepScreen() {
  this.getActivity().runOnUiThread(new Runnable() {
   
      public void run() {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    keep= true;
      }
  });
};

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 30, 2019, 10:53am UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/12 "2019-10-30T10:53:23Z")

</div>

Perfect! Simple and objective. Thanks

---

<div class="post-metadata">

**Author:** ![J\_Emmerson](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@J\_Emmerson](https://discourse.processing.org/u/J_Emmerson)\
**Post date:** [October 30, 2019, 2:20pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/13 "2019-10-30T14:20:04Z")

</div>

Could you just give me the necessary code that I can copy and paste because this doesn’t work either

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 31, 2019, 12:28pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/14 "2019-10-31T12:28:22Z")

</div>

@J_Emmerson Tested on Lollipop 5.1 and KitKat 4.4  
Please post your Android version and if it gives any error.  
PS You can set screen time to 15 seconds in Settings just to test

```auto
import android.os.Bundle;
import android.view.WindowManager;
import android.app.Activity;
boolean keep = false;
Activity act;

void setup() {
  size(600, 600);
  orientation(PORTRAIT);
  background(255, 0, 0);
}

void draw() {
  background(0, 0, 255);
  textSize(24);
  textAlign(CENTER);
  if (keep) {
    text("No Sleep", width/2, height/2);
  } else {
    text("Will Sleep", width/2, height/2);
  }
}

private void KeepScreen() {
  act.runOnUiThread(new Runnable() {
    public void run() {
      act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      keep= true;
    }
  }
  );
}

@Override
  public void onStart() {
  super.onStart();
  act = this.getActivity(); 
  if (!keep) {
    KeepScreen();
  }
}

@Override
  public void onResume() {
  super.onResume();
  if (!keep) {
    KeepScreen();
  }
}

@Override
  public void onStop() {
  super.onStop();
  keep = false;
}

```

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [October 31, 2019, 12:51pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/15 "2019-10-31T12:51:42Z")

</div>

It worked for me just adding this:

Edited: Forgot to take out the protected tag

```auto
    @Override
    void onResume() {
        getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onResume();
    }

    @Override
    void onPause() {
        getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onPause();
    }

```

[🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️](https://discourse.processing.org/t/evitar-la-atenuacion-del-brillo-al-cabo-de-un-rato-as/13796/3) [🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️](https://discourse.processing.org/t/prevent-sleep-in-android/15011/3)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 31, 2019, 1:02pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/16 "2019-10-31T13:02:52Z")

</div>

I have to link an activity to getWindow otherwise I get the error:  
The method getWindow() is undefined for the type sketch\_20191006b

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [October 31, 2019, 1:12pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/17 "2019-10-31T13:12:05Z")

</div>

In my tablet Android 6.0 Marshmallow, don’t need to add anything

 ![Captura%20de%20pantalla%20de%202019-10-31%2014-07-34](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/442362bb43697d2ded1871565aac1ac392bea4f5.png)

In the version for AS just the onResume and onPause had the tag protected

I tried too in android 8 and 9 and works too

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 31, 2019, 1:20pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/18 "2019-10-31T13:20:16Z")

</div>

Good to know!  
Would you be so kind to test akenaton"s code that I also had to modify slightly and posted right here above, on your MM ?

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [October 31, 2019, 1:39pm UTC](https://discourse.processing.org/t/prevent-sleep-in-android/15011/19 "2019-10-31T13:39:12Z")

</div>

Yep, I tried it and works fine too in my MM
