I am working on an android app that listens to the surrounding voices, and executes commands.
One of the commands is waking up the screen.
In order to achieve that goal, I am using the following function, inside my service:
private void wakeupScreen() {
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
fullWakeLock.acquire(); // turn on
try {
Thread.sleep(10000); // turn on duration
} catch (InterruptedException e) {
e.printStackTrace();
}
fullWakeLock.release();
} catch (Exception e) {
return e;
}
return null;
}
}.execute();
}
However, I have several problems with this code: (it runs on a service)
Using
SCREEN_BRIGHT_WAKE_LOCK
andFULL_WAKE_LOCK
is deprecated.The use of AsyncTask and sleep seems like a bad solution for turning the screen on, in non-blocking way.
I wonder if more elegant way exists. Any suggestions?