How to find the screen is locked in android

后端 未结 6 1326
猫巷女王i
猫巷女王i 2020-12-03 11:47

For my application, I need to know that the screen is locked. How to check this is problematically. I used following flag:

if(WindowManager.LayoutParams.FLAG         


        
6条回答
  •  暖寄归人
    2020-12-03 12:11

    I guess you may have already found the answer, but if not (and for other developers), you can do it like this:

                  PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
                  boolean isScreenOn = powerManager.isScreenOn();
    
    if (!isScreenOn) {
               //Screen is in OFF State
               //Code to power on and release lock 
    
    
    
                    KeyguardManager km = (KeyguardManager) this
                     .getSystemService(Context.KEYGUARD_SERVICE);
                   final KeyguardManager.KeyguardLock kl = km
                     .newKeyguardLock("MyKeyguardLock");
                   kl.disableKeyguard();
    
                   PowerManager pm = (PowerManager) this
                     .getSystemService(Context.POWER_SERVICE);
                   WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                     | PowerManager.ACQUIRE_CAUSES_WAKEUP
                     | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
                   wakeLock.acquire();
    }
    

提交回复
热议问题