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!