Prevent sleep in android

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

Hi. I think you need WakeLock (reference)

And also set it as a permission.

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

In this case you don’t need permission


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

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

I tried this code and my sketch didn’t work

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

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

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

@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.

@noel ===

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


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

Perfect! Simple and objective. Thanks

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

@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

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

It worked for me just adding this:

Edited: Forgot to take out the protected tag

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

:man_facepalming::man_facepalming::man_facepalming::man_facepalming::man_facepalming::man_facepalming: :man_facepalming::man_facepalming::man_facepalming::man_facepalming::man_facepalming::man_facepalming::man_facepalming::man_facepalming:

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

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

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

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 ?

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

1 Like