Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn screen on during partial wake lock

My activity runs with a partial wake lock because it is continually handling received Bluetooth data. The wake lock is set up like so:

  powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
  wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
  wakeLock.acquire();

When certain events occur I want to on the display so the user can view the status. I want the display to turn on automatically, not by a user keypress. However all attempts to do this have failed. When the display turns off while running with a partial wake lock, my attempts to turn the display back and let the user see the current activity have failed. I have tried faking user activity:

PowerManager NewPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
   NewPowerManager.userActivity(1, false);

Setting flags for the window:

 Window win = getWindow();
  win.setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

And even releasing the wake lock and starting another that should turn on the screen:

if (wakeLock != null)
   {
      wakeLock.release(); // release the wakelock
   }
PowerManager.WakeLock TempWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK |
   PowerManager.ACQUIRE_CAUSES_WAKEUP, "TempWakeLock");
   // tried | PowerManager.ON_AFTER_RELEASE to no avail
   TempWakeLock.acquire();

Is there something that I am missing here? I am not trying to open up a new activity, but merely displaying my current one to the user. Has anyone else been able to do this? Thanks for any help you can give me.

like image 471
Tary Avatar asked Apr 27 '12 14:04

Tary


People also ask

What is stuck partial wake locks?

A partial wake lock becomes stuck if it is held for a long time while your app is running in the background (no part of your app is visible to the user). This condition drains the device's battery because it prevents the device from entering lower power states.

How do I keep my Android screen awake?

Open Settings. Tap Display. Tap Sleep or Screen timeout. Select how long you want your Android smartphone or tablet screen to stay on before turning off due to inactivity.

What is Wakelock screen?

Wake locks allow your application to control the power state of the host device. Creating and holding wake locks can have a dramatic impact on the host device's battery life. Thus you should use wake locks only when strictly necessary and hold them for as short a time as possible.


1 Answers

I was finally able to resolve this issue after seeing some demo code from Lance at HTC (thanks by the way).

The secret seems to be to create an additional full wake lock with ACQUIRE_CAUSES_WAKEUP set. I don't release the original partial wake lock, but when I want to turn on the display I create the new full wake lock, do my stuff, then release the new full wake lock. This actually works!

PowerManager TempPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock TempWakeLock = TempPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | 
PowerManager.ON_AFTER_RELEASE, "TempWakeLock");
TempWakeLock.acquire();

// do the work that needs the visible display...

// Release the newest wakelock and fall back to the old one
TempWakeLock.release();
like image 75
Tary Avatar answered Oct 04 '22 00:10

Tary