生物识别提示适用于 API 21+ 但 API 19 显示错误

Biometrics prompt works with API 21+ but API 19 shows error

所以我在使用设备锁定方法的应用程序上锁定了应用程序 - 我使用支持所有 android 版本的 Biometrics 1.0.1,但是在 KitKat 上,当我按下解锁时,它显示 tihs toast:

authentication error: This device does not have a fingerprint sensor.

即使我使用 .setDeviceCredentialAllowed(true) 如果没有指纹可用,它应该使用设备 PIN 或图案。

keyguardManager.isKeyguardSecure returns 真

promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric login for my app")
            .setSubtitle("Log in using your biometric credential")
            .setDeviceCredentialAllowed(true)
            .build()

        // Prompt appears when user clicks "Unlock".
        val biometricLoginButton = unlock_button
        biometricLoginButton.setOnClickListener {
            biometricPrompt.authenticate(promptInfo)
        }

setDeviceCredentialAllowed()方法只支持Api21+

因为这个 api 内部使用 KeyguardManager.createConfirmDeviceCredentialIntent(CharSequence, CharSequence) 此方法在 API 21 添加并在 API 29 Reference

弃用

来自Api docs

On versions P and below, once the device credential prompt is shown, BiometricPrompt.cancelAuthentication() will not work, since the library internally launches KeyguardManager.createConfirmDeviceCredentialIntent(CharSequence, CharSequence), which does not have a public API for cancellation.