3

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 and FULL_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?

barak1412
  • 972
  • 7
  • 17

2 Answers2

2

Apparently, there is no elegant way to achieve that goal.

I will summarize my 3 non-elegant solutions to that problem, for future readers:

  • Use wakelocks in asynchronously way:

    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();
    }
    
  • Launch an activity that turns the screen on.

  • Simulate HOME button click. (similar to solution 2, without the need to create a custom activity, and slightly different behavior)

I found the first solution as the best for my needs.

Community
  • 1
  • 1
barak1412
  • 972
  • 7
  • 17
1

Launch an activity, just to get the screen on under onCreate().

See the discussions at what is the proper, non-deprecated way to wake up the device?

Community
  • 1
  • 1
headuck
  • 2,763
  • 16
  • 19