1

以下是我检测生物识别功能的代码

class MainActivity : AppCompatActivity() {

    private var executor: Executor? = null
    private var biometricPrompt: BiometricPrompt? = null
    private var promptInfo: BiometricPrompt.PromptInfo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bioMetricManager = BiometricManager.from(this)

        when (bioMetricManager.canAuthenticate()) {
            BiometricManager.BIOMETRIC_SUCCESS -> {
                executor = ContextCompat.getMainExecutor(this)

                promptInfo = BiometricPrompt.PromptInfo.Builder().setTitle("Biometric login for leaao")
                    .setSubtitle("Log in using biometric credentials").setDeviceCredentialAllowed(true).build()

                biometricPrompt = BiometricPrompt(this,
                    executor!!, object : BiometricPrompt.AuthenticationCallback() {
                        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                            super.onAuthenticationError(errorCode, errString)
                            Log.i("here4","Authentication error: $errString")
                        }

                        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                            super.onAuthenticationSucceeded(result)
                            Log.i("here5","Success")
                        }

                        override fun onAuthenticationFailed() {
                            super.onAuthenticationFailed()
                            Log.i("here6","Authentication failed: ")
                        }
                    })

                biometricPrompt?.authenticate(promptInfo!!)
            }
            BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
                Log.i("here","No biometric features available on this device.")
            }
            BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
                Log.i("here2","Biometric features are currently unavailable.")
            }
            BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
                Log.i("here3","The user hasn't associated any biometric credentials with their account.")
            }
        }
    }
}

我已添加 <uses-permission android:name="android.permission.USE_BIOMETRIC" />到清单文件并添加implementation 'androidx.biometric:biometric:1.0.1'到 gradle

我的代码仍然无法检测到生物识别提示。我已经在我的手机上启用了人脸识别,但是当我打开我的应用程序时它不起作用。它适用BIOMETRIC_ERROR_HW_UNAVAILABLE于我的代码。

4

0 回答 0