我最近将我的项目转移到了AndroidX,在为应用程序实现指纹的同时,我正在使用AndroidX的生物识别技术。
implementation 'androidx.biometric:biometric:1.0.0-alpha03'
当显示使用指纹进行身份验证的对话框时,对话框将"取消"选项设置为负按钮。
final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Log into App")
.setSubtitle("Please touch the fingerprint sensor to log you in")
.setDescription("Touch Sensor")
.setNegativeButtonText("Cancel".toUpperCase())
.build();
根据安卓文档: https://developer.android.com/reference/androidx/biometric/BiometricPrompt.PromptInfo.Builder.html#setNegativeButtonText(java.lang.CharSequence)
Required: Set the text for the negative button.
This would typically be used as a "Cancel" button, but may be also used
to show an alternative method for authentication,
such as screen that asks for a backup password.
因此,我可以说"使用密码"来提供另一种方法,以防指纹失败,而不是"取消"按钮,当用户单击它时,我可以显示另一个弹出对话框,用户可以在其中输入设备密码以帮助从密钥库中检索应用程序密码。这是对的吗?
但是,如果我没有设置密码来解锁手机,而是使用模式,会发生什么?
我看到如果我使用 android.hardware.biometrics.Biometric.BiometricPrompt.Builder 而不是 androidx.biometric.BiometricPrompt.PromptInfo.Builder,它有一个方法 https://developer.android.com/reference/android/hardware/biometrics/BiometricPrompt.Builder.html#setDeviceCredentialAllowed(布尔) 出于同样的目的,让用户在指纹失败时使用其他方式进行身份验证。
有人可以帮助我理解这一点吗?我如何使用 AndroidX 实现这一目标,因为我的应用程序从 API 16 开始兼容。为什么AndroidX没有返回这种回退方法?
setDeviceCredentialAllowed API 最近在 beta01 中添加
在此处查看发行说明
https://developer.android.com/jetpack/androidx/releases/biometric
在 SDK 版本 Q 及更高版本上使用带有身份验证回调的BiometricPrompt
,否则使用createConfirmDeviceCredentialsIntent
。
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val biometricPrompt = BiometricPrompt.Builder(this)
.setTitle(getString(R.string.screen_lock_title))
.setDescription(getString(R.string.screen_lock_desc))
.setDeviceCredentialAllowed(true)
.build()
val cancellationSignal = CancellationSignal()
cancellationSignal.setOnCancelListener {
println("@Biometric cancellationSignal.setOnCancelListener")
//handle cancellation
}
val executors = mainExecutor
val authCallBack = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
super.onAuthenticationError(errorCode, errString)
print("SecuritySetupActivity.onAuthenticationError ")
println("@Biometric errorCode = [${errorCode}], errString = [${errString}]")
//handle authentication error
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
super.onAuthenticationSucceeded(result)
print("SecuritySetupActivity.onAuthenticationSucceeded ")
println("@Biometric result = [${result}]")
//handle authentication success
}
override fun onAuthenticationHelp(helpCode: Int, helpString: CharSequence?) {
super.onAuthenticationHelp(helpCode, helpString)
print("SecuritySetupActivity.onAuthenticationHelp ")
println("@Biometric helpCode = [${helpCode}], helpString = [${helpString}]")
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
print("SecuritySetupActivity.onAuthenticationFailed ")
//handle authentication failed
}
}
biometricPrompt.authenticate(cancellationSignal, executors, authCallBack)
} else {
val i = km.createConfirmDeviceCredentialIntent(getString(R.string.screen_lock_title), getString(R.string.screen_lock_desc))
startActivityForResult(i, 100)
}
在 BiometricPromopt 上尝试 setDeviceCredentialAllowed(true)。
> androidx 1.0.0 允许您轻松设置回退 - 如下所示:
// Allows user to authenticate using either a Class 3 biometric or
// their lock screen credential (PIN, pattern, or password).
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
// Can't call setNegativeButtonText() and
// setAllowedAuthenticators(... or DEVICE_CREDENTIAL) at the same time.
// .setNegativeButtonText("Use account password")
.setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
.build()
看到这里