我最近将项目移至AndroidX,并且在为应用程序实现指纹时,我正在使用Biometric for 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();
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.BiometricPrompt.Builder而不是androidx.biometric.BiometricPrompt.PromptInfo.Builder,它具有方法https://developer.android.com/reference/android/hardware/biometrics/BiometricPrompt.Builder.html#setDeviceCredentialAllowed(boolean) 出于相同的目的,如果指纹失败,则允许用户使用其他方式进行身份验证。
有人可以帮助我理解吗?由于我的应用从API 16开始兼容,因此我如何使用AndroidX实现此目标。为什么AndroidX不使用此后备方法返回?
答案 0 :(得分:4)
最近在beta01中添加了setDeviceCredentialAllowed API
在此处查看发行说明
https://developer.android.com/jetpack/androidx/releases/biometric
答案 1 :(得分:0)
在BiometricPromopt上尝试setDeviceCredentialAllowed(true)。
答案 2 :(得分:0)
在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)
}
答案 3 :(得分:0)
androidx 1.0.0允许您轻松设置后备-像这样:
// Allows user to authenticate using a PIN, pattern, or password.
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Cancel")
.setDeviceCredentialAllowed(true)
.build()
请参阅this