使用生物特征识别时如何打开/关闭身份验证指纹或面部识别

问题描述

我使用Biometric来验证指纹或面部识别。工作正常!但是我只想使用指纹,或者如果我的设备同时设置为两者,则只想使用面部识别。 我可以做吗?以及如果可以的话我该怎么办? 这是我的代码

@RequiresApi(api = Build.VERSION_CODES.P)
public void authenticateUser(@NonNull Activity activity) {
    BiometricPrompt biometricPrompt = new BiometricPrompt.Builder(activity)
            .setTitle("Biometric Demo")
            .setSubtitle("Authentication is required to continue")
            .setDescription("This app uses biometric authentication to protect your data.")
            .setNegativeButton("Cancel",activity.getMainExecutor(),(dialogInterface,i) -> {
                        mCallback.onCancel();
                    })
            .build();

    biometricPrompt.authenticate(mCancellationSignal,getAuthenticationCallback());
}

@RequiresApi(api = Build.VERSION_CODES.P)
private BiometricPrompt.AuthenticationCallback getAuthenticationCallback() {

    return new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode,CharSequence errString) {
            super.onAuthenticationError(errorCode,errString);
            mCallback.onError();
        }

        @Override
        public void onAuthenticationHelp(int helpCode,CharSequence helpString) {
            super.onAuthenticationHelp(helpCode,helpString);
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
        }

        @Override
        public void onAuthenticationSucceeded(
                BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            mCallback.onAuthenticated();
        }
    };
}

解决方法

如果我的设备设置为两者,则我只希望使用指纹或仅使用面部识别。我可以做吗?

答案:根据最新的API,您不能这样做。

不同设备之间的生物识别功能不一致。在我的设备存储库中,我有Samsung S10设备和MI设备,它们的行为不同。 在Samsung S10设备中,我只能在设备设置中设置FACE / FINGERPRINT。当我调用身份验证API时,设备设置中的任何设置都会生效。

在MI设备中,除了“仅面部识别/仅指纹识别”功能外,我还可以选择同时设置两者。我认为这与您的情况相同。如果我在设备设置中同时设置了这两个选项,那么authenticate()之后,我可以使用Face或Fingerprint进行身份验证。