简体   繁体   中英

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM