How to use Partial WakeLock

I am trying to make an audio based game in which the phones screen is primarily off. To be specific I need the app to continue to run, playing sounds and communicating with a server, even when I turn it off via the power button.

my first semi-solution was to keep the screen on using this: (executed in setup)

  runOnUiThread(new Runnable() {
    @ Override
      public void run() {
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
  }
  );
//unfortunately I lost the source where I found this :-( 

However I would like the app to also “survive” the power button being pressed. From my research so far (e.g. https://developer.android.com/training/scheduling/wakelock#java) it seems that I need a “partial wakeLock.”

I have tried various variations of the following with no positive results:

import android.os.PowerManager.WakeLock;
import android.content.Context;
import android.os.PowerManager;



void setup() {
  requestPermission("android.permission.WAKE_LOCK");
  runOnUiThread(new Runnable() { //dark screen magic
    @ Override
      public void run() {
      PowerManager pm = (PowerManager)getActivity().getSystemService(Context.POWER_SERVICE);
      WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
      wakeLock.acquire();
    }
  }
  );
}

int c;
void draw() {
  println(c);  //counter to test responsiveness
  c++;
  delay(1000);
}

Does anyone know how to implement the WakeLock correctly?
Thanks for any help!

Hi, @Jessie welcome to the forum, and sorry for the late answer.
I’ve looked into this, but do you really need it?
If you use a counter in draw() the counting will stop, but if you use a thread() like in the code below, it will continue counting even when the power button pressed.
Also, the sounds of the cassette library will continue.
Maybe you can post a code that you not want to be stopped after power button pressed.

int count; 
boolean foo; 
int start_time; 

void setup() { 
  background(0, 0, 200); 
  textSize(60); 
  thread("count");
} 
void draw() { 
  background(0, 0, 200); 
  text(count, width/2, height/2); 
} 

void count() { 
  while (!foo) { 
    if (millis() - start_time > 1000) { 
      count++; 
      start_time = millis();
    }
  }
}
1 Like

Huh, I was not aware of the existence of “threads” in Processing. This did indeed solve my problem without the use of a wakelock as far as I can tell. Thanks a ton!

One more quick question because I am new here: If I do end up running into problems with this solution later on, should I start a new thread or continue this one?

Again thanks for the answer and happy coding! :slight_smile:

You can just post it here.
I will add a WakeLock code to my repo in the near future, but at the moment I’m quite busy to make the native android codes posted there, suitable for the latest Android versions (10&11) as well.

@noel, @Jessie ====
The answer and the code from @noel is useful for threading in android, which is a very good practice as Android does not like long or heavy tasks in the main ui thread; now i am not sure that this answer and this code are a solution if the user presses (as it il is explained in the post) the power management button: in this case i guess that the created thread will be killed when the phone is spent. this is a case where the partial wakeLock could be used though Android and Android doc does not like it, for battery draining reasons. As the code is very simple i dont put it; of course you have to ask permission; of course also ou have to think ro "release " the wavelock when it is not required