检查Android屏幕是否锁定
每当安卓设备(运行安卓4.0以后的版本)被锁定或解锁时,我都需要做些什么。所需条件如下:检查Android屏幕是否锁定,android,keyguard,Android,Keyguard,每当安卓设备(运行安卓4.0以后的版本)被锁定或解锁时,我都需要做些什么。所需条件如下: 如果屏幕锁定设置为“否”,则在按下电源按钮和屏幕关闭时,不认为设备被锁定。 如果屏幕锁被设置为“否”以外的任何东西,我认为当键盘保护屏不存在时,该设备将被解锁。 我已经实现了这段代码,它似乎适用于Android 5.0,并且在使用“None”时会考虑到这一点。我还检查了其他问题,例如在发布此问题之前 private class KeyguardWatcher extends BroadcastRecei
- 如果屏幕锁定设置为“否”,则在按下电源按钮和屏幕关闭时,不认为设备被锁定。
- 如果屏幕锁被设置为“否”以外的任何东西,我认为当键盘保护屏不存在时,该设备将被解锁。
private class KeyguardWatcher extends BroadcastReceiver {
public KeyguardWatcher() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_USER_PRESENT);
MyService.this.registerReceiver(this, intentFilter);
}
public void destroy() {
MyService.this.unregisterReceiver(this);
}
@Override
public void onReceive(Context context, Intent intent) {
// Old Android versions will not send the ACTION_USER_PRESENT intent if
// 'None' is set as the screen lock setting under Security. Android 5.0
// will not do this either if the screen is turned on using the power
// button as soon as it is turned off. Therefore, some checking is needed
// with the KeyguardManager.
final String action = intent.getAction();
if (action == null) {
return;
}
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
// If 'None' is set as the screen lock (ie. if keyguard is not
// visible once the screen goes off), we do not consider the
// device locked
if (mKeyguardManager.inKeyguardRestrictedInputMode()) {
doStuffForDeviceLocked();
}
} else if (action.equals(Intent.ACTION_SCREEN_ON)) {
if (!mKeyguardManager.inKeyguardRestrictedInputMode()) {
// The screen has just been turned on and there is no
// keyguard.
doStuffForDeviceUnlocked();
}
// If keyguard is on, we are to expect ACTION_USER_PRESENT when
// the device is unlocked.
} else if (action.equals(Intent.ACTION_USER_PRESENT)) {
doStuffForDeviceUnlocked();
}
}
}
这似乎在安卓5.0中对我有效。然而,我想知道,在处理操作屏幕关闭时,这是否可能容易出现竞争条件。是否可能正在使用除“无”之外的其他内容(例如“刷卡”),并且在我处理操作屏幕\u OFF
键盘保护时,键盘保护未处于受限输入模式,但很快就会处于受限输入模式?如果是这样的话,我永远不会认为设备锁定,但它可能是。