我想要实现的:提供了使用FingerPrint、FaceId和Iris等不同生物特征登录的选项。我想给出选择使用PIN,密码或模式,以防任何生物传感器不工作。
"onAuthenticationError“问题:当用户单击"Use Password”选项时,它将直接转到BiometricPrompt.ERROR_NEGATIVE_BUTTON.的错误代码检查的回调我关心的是,我需要自己来处理吗?我的意思是,我是否需要显示一个dialogPopUp,我将要求用户输入他/她的电子邮件/用户名和密码,然后他/她可以登录我的应用程序?
我所做的:
依赖关系:
implementation 'androidx.biometric:biometric:1.2.0-alpha01'MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var executor: Executor
private lateinit var biometricPrompt: BiometricPrompt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
executor = ContextCompat.getMainExecutor(this)
biometricPrompt = createBiometricObject()
}
private fun createBiometricObject(): BiometricPrompt {
return BiometricPrompt(this, executor, object : AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
if (errorCode == ERROR_NEGATIVE_BUTTON && errString == "Use Password") {
// Do I need to create my own DialogPopUp?
}
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
}
})
}
fun loginWithBiometrics(view: View) {
when (BiometricManager.from(this).canAuthenticate(BIOMETRIC_STRONG)) {
BiometricManager.BIOMETRIC_SUCCESS -> biometricPrompt.authenticate(
createPromptInfoForBiometrics()
)
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> Toast.makeText(
this,
"Please enroll your biometrics",
Toast.LENGTH_LONG
).show()
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> Toast.makeText(
this,
"Device not compatible",
Toast.LENGTH_LONG
).show()
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> Toast.makeText(
this,
"Sensors are available as off now, please try again later!",
Toast.LENGTH_LONG
).show()
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
TODO()
}
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> {
TODO()
}
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
biometricPrompt.authenticate(createPromptInfoForBiometrics())
}
}
}
private fun createPromptInfoForBiometrics(): BiometricPrompt.PromptInfo {
return BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric Login")
.setSubtitle("Please login with your biometrics")
.setNegativeButtonText("Use Password")
.setAllowedAuthenticators(BIOMETRIC_STRONG)
.build()
}
}任何帮助都将不胜感激。
发布于 2022-01-04 11:55:04
您应该尝试将DEVICE_CREDENTIAL标志传递给setAllowedAuthenticators()。
BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
//..
.setAllowedAuthenticators(DEVICE_CREDENTIAL or BIOMETRIC_STRONG)
.build()但也要确保您在适当的android级别上使用它。根据医生的说法:
注意,在Android11 (API 30)之前,并不支持所有身份验证器类型的组合。具体来说,在API 30之前,仅不支持DEVICE_CREDENTIAL,在API28-29上不支持BIOMETRIC_STRONG \ DEVICE_CREDENTIAL。在受影响的Android版本上设置不受支持的值将导致调用build().
时出现错误。
https://stackoverflow.com/questions/65790648
复制相似问题