问题描述
我想要实现的目标: 提供使用指纹、FaceId 和 Iris 等不同生物识别信息登录的选项。我想提供使用 PIN、密码或图案的选项,以防任何生物识别传感器不起作用。
问题:当用户点击“使用密码”选项时,它直接转到“onAuthenticationError”回调,我正在检查错误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()
}
}
任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)