1

I am having trouble with wakelocks. Basically, I had the wakelock running in my timer thread, a doInBackground of an AsyncTask for the entire duration of my app (it is a background app for taking performance measurements). Then I decided I only want the screen to wakeup every 10 minutes or so for a second or so. So I created another class extending AsyncTask and put the code below into it's doInBackground, but now the screen doesn't turn back on. I should note that I start this thread and two other threads that are AsyncTask with doInBackground methods from onCreate.

Here is my new inner class doing the waking up: Essentially all it is supposed to do is wake the phone screen up every 10 minutes for a bit until my other two background threads set their booleans true.

private class WakeUp extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());

        do{
            try {
                Thread.sleep(WAKEUP_EVERY); //600000ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }           

            wl.acquire();

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            wl.release();

        }while(!timeCompleted || !transferCompleted);

        return null;
    }

}
Michael
  • 85
  • 1
  • 7
  • Are you sure the thread isn't killed? You might want to use a service for this or the AlarmService to make a pulse for every 10 minutes. As AsyncTask runs on a Threadpool for your app that threadpool probably has a timeout on thread executions. – Greg Giacovelli Feb 19 '11 at 05:56
  • @Greg I just added a log statement right after acquiring the wakelock and one right before releasing it to see if the tread was still alive. The statements show up in adb but the screen doesn't power on, so it appears as if the thread is alive but the waklock is not functioning as expected. I will look into the Service/AlarmService and see if there is something to try. – Michael Feb 19 '11 at 06:49
  • OH, have you added this flag, ACQUIRE_CAUSES_WAKEUP. If you want the screen to turn on, I believe you will have to add that flag, PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP – Greg Giacovelli Feb 19 '11 at 06:56
  • http://stackoverflow.com/questions/3689878/wakelock-is-not-turning-on-the-screen-help – Greg Giacovelli Feb 19 '11 at 07:01
  • Thanks Greg, that solved the problem! If you want to make a post in the answer area I'll be sure to give you credit for your response. Now hopefully this keeps my device from going to sleep so my connections don't close... – Michael Feb 19 '11 at 07:13

1 Answers1

2

You've forgotten to tell the wake lock to turn on the screen using the ACQUIRE_CAUSES_WAKEUP flag. As per the documentation:

Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

See ACQUIRE_CAUSES_WAKEUP for more details :D

Dylan Knowles
  • 2,726
  • 1
  • 26
  • 52