简体   繁体   中英

Android - cannot re-enable keyguard while screen is off

I'm trying to get my app to reenable the keyguard after x amount of time passes (using alarm manager). Basically, here is what I do:

Let's say I set the screen lock to be enabled after 5 minutes.

  1. My app starts and I disable the keyguard - works great!
  2. I create a timer(using alarm manager) to lock the screen after 3 minutes and I see in the logs that the alarm goes off in 3 minutes and the code for re-enabling the keyguard kicks off. Screen goes off after 5 minutes and I click the power button and keyguard is enabled - works great! Basically, everything works fine as long as everything is done while the screen is on.

Now let's say we keep the same scenario as above, but change step 2 to:

  1. Same as above
  2. I create a timer(using alarm manager) to lock the screen after 10 minutes. Screen goes off after 5 minutes and I still see in the logs that the alarm goes off correctly after 10 minutes and re-enables the keyguard. However, when I click the power button after that, I can still enter the app without encountering the lock screen. If I either click the power button or wait for the screen to go off again, then I will encounter the lock screen.

This seems to indicate to me that re-enabling the keyguard only takes affect after the screen goes on again and you cannot enable it while the screen is off. Am I wrong or is there another way around this? I would like to be able to kick the user back to the lock screen even if the code re-enables the lock guard while the screen is off.

Thanks!

Answer was to use:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, this.getClass().getSimpleName());
wl.acquire();

before re-enabling the keyguard

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