最近因为一些事接触到android屏幕解锁这块,刚开始查询网上资料,绝大部分以keyguardLock 来进行获取屏幕锁和接触屏幕锁,其思路如下:
// KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
// KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("glapp");
// keyguardLock.disableKeyguard();
获取当前屏幕锁并解锁;
// KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
// KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("glapp");
// keyguardLock.reenableKeyguard();
然后在相应位置 释放持有的屏幕锁
但是这个思路带一个吻,在最后哦一次退出应用的时候由于释放的屏幕锁被系统回收,系统会再次锁屏,造成不好的用户体验,
经过翻阅api,发现有一个更好的解决方式:
在window对象中,存在这样一个flags :WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
位于 package android.view.WindowManager中。
其注释如下:
/** Window flag: when set the window will cause the keyguard to
* be dismissed, only if it is not a secure lock keyguard. Because such
* a keyguard is not needed for security, it will never re-appear if
* the user navigates to another window (in contrast to
* {@link #FLAG_SHOW_WHEN_LOCKED}, which will only temporarily
* hide both secure and non-secure keyguards but ensure they reappear
* when the user moves to another UI that doesn't hide them).
* If the keyguard is currently active and is secure (requires an
* unlock pattern) than the user will still need to confirm it before
* seeing this window, unless {@link #FLAG_SHOW_WHEN_LOCKED} has
* also been set.
*/
public static final int FLAG_DISMISS_KEYGUARD = 0x00400000;
其大意为: