KeyguardManager - api 25及以下的requestDismissKeyguard()方法替代[Android]

时间:2018-05-24 11:42:01

标签: android keyguard keyguardlock

我最近试图请求用户从我的应用程序手动解除密钥保护。当设备被锁定且屏幕上有标志

时,我的应用会调用活动屏幕

FLAG_SHOW_WHEN_LOCKED
FLAG_TURN_SCREEN_ON

当我必须调用另一个没有这些标志的屏幕时,我想请求用户解锁密钥保护,这种行为可以在相机应用程序中看到 - 当我们想要分享在手机拍摄的照片时被锁定它将要求我们解锁设备。

requestDismissKeyguard()方法仅适用于api26及以上任何替代品,适用于较低的apis ??

2 个答案:

答案 0 :(得分:0)

您可以在API级别21中使用createConfirmDeviceCredentialIntent

https://developer.android.com/reference/android/app/KeyguardManager.html#createConfirmDeviceCredentialIntent(java.lang.CharSequence,%20java.lang.CharSequence)

样本:

//region [in some funtion]
if (keyguardManager.isKeyguardLocked()) {
   Intent intent = keyguardManager.createConfirmDeviceCredentialIntent("My Title", "A Custom Description");
   if (intent != null) {
       startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
   }else{
        Toast.makeText(MainActivity.this, "Secure lock screen hasn't set up", Toast.LENGTH_SHORT).show();
   }
}
//endregion

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
       if (resultCode == RESULT_OK) {
           Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
       } else {
           Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
       }
    }
}

答案 1 :(得分:0)

您可以将标志用于较低版本

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);