众所周知,从 android 9.0 开始,android 引入了BiometricPrompt Api,以在越来越多的生物识别传感器(例如指纹、Face ID 等)中提供标准的身份验证体验。
现在有了这个新的 BiometricPrompt Api,用户可以通过指纹、面部扫描仪或虹膜扫描进行身份验证(取决于他们的生物识别偏好)。BiometricPrompt api 会处理这个问题,它会通过各种回调通知我们。
下面是我显示生物识别提示的代码。
biometricPrompt = new BiometricPrompt.Builder(context)
.setTitle("FingerPrint Authentication")
.setSubtitle("Login via Fingerprint")
.setDescription("Touch Fingerprint Sensor")
.setNegativeButton("Cancel", context.getMainExecutor(),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.d(TAG,"Cancelled");
}
})
.build();
Run Code Online (Sandbox Code Playgroud)
现在,如果您看到我的代码,我将标题设置为Fingerprint Authentication。现在在设备设置中,如果用户已将生物识别首选项设置为面容 ID而不是指纹,那么此biometricPrompt将通过面容ID对用户进行身份验证,即使用户一直触摸传感器,指纹传感器也不会工作。这会造成混淆,因为生物识别标题说“指纹身份验证”和用户实际上是通过faceID 进行身份验证的
有什么方法可以让我们知道用户选择了什么生物识别偏好(例如指纹或面容 ID)?因此,基于该偏好,我可以在 BiometricPrompt 上显示适当的消息,这样用户就不会感到困惑。
我已经从 BiometricPrompt 探索了所有 api,但可以找到任何与 BiometricPreference 相关的东西。
任何帮助将不胜感激。
android samsung-mobile android-fingerprint-api android-9.0-pie android-biometric-prompt
我BiometricPrompt在我的应用程序中使用。它运行良好并在调用该authenticate()方法时显示对话框。但是当我在对话框外单击时,此对话框将关闭。如何预防?如何使 BiometricPrompt 的对话框不可取消?这里没有像biometricPrompt.setCancelable(false).
如果您在活动中创建 biometricPrompt 和 promptInfo,它工作正常。但我无法让它在片段中工作。
这是在一个片段内,它在 OnViewCreated 内被调用。你在一个很好的活动中做同样的事情,一种解决方案是从活动中传递 biometricPrompt 和 PromptInfo 并将它传递到片段中。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tryToDisplayBiometricPrompt()
}
@TargetApi(Build.VERSION_CODES.M)
private fun tryToDisplayBiometricPrompt() {
//Create a thread pool with a single thread
biometricPrompt = BiometricPrompt(activity as FragmentActivity, Executors.newSingleThreadExecutor(), object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
authenticationSuccessful()
}
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
if (errorCode == BiometricConstants.ERROR_NEGATIVE_BUTTON || errorCode == BiometricConstants.ERROR_USER_CANCELED || errorCode == BiometricPrompt.ERROR_CANCELED) return
authenticationlistener?.isBiometricAvailable = false
authenticationlistener?.onAuthenticationFailed()
} …Run Code Online (Sandbox Code Playgroud) 我是 espresso 测试的新手,我想为 BiometricPrompt 编写 Espresso 测试,BiometricPrompt 是 Google 提供的用于验证指纹的 API。
我使用 setDeviceCredentialAllowed(true) 实现了新的生物识别库,如官方文档中所示
在这个库中,有一个活动正在使用“DeviceCredentialHandlerActivity”,这个活动在清单中已经导出=true,为什么这是/这真的需要?众所周知,出于安全原因,应尽可能避免出口活动。我已经用以下方法覆盖了该属性:
<activity android:name="androidx.biometric.DeviceCredentialHandlerActivity"
android:exported="false"
tools:replace="android:exported">
</activity>
Run Code Online (Sandbox Code Playgroud)
使用指纹/密码进行身份验证仍在 Android 29 及以下版本上工作。
我需要使用指纹和人脸身份验证来集成生物识别身份验证。指纹认证工作完美,但当我只设置人脸认证时,我从 BiometricManager.from(context) 方法得到生物识别未注册响应,如下所示,
val biometricManager = BiometricManager.from(context)
when(biometricManager.canAuthenticate()){
BiometricManager.BIOMETRIC_SUCCESS ->{
Log.e(TAG, "App can authenticate using biometrics.")
}
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->{
Log.d(TAG, "Hardware not available")
}
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->{
Log.d(TAG, "Biometric features are currently unavailable.")
}
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED ->{
Log.d(TAG, "The user hasn't associated any biometric credentials with their account.")
}
else ->{
Log.d(TAG, "Nothing supported")
}
}
Run Code Online (Sandbox Code Playgroud) 我们正在更改应用程序内的语言环境,除了指纹对话框中的提示外,一切正常。无论我们设置什么语言,我们总是有英文提示:
private fun setNewLocaleAndRestart(language: String) {
LocaleManager(this).setNewLocale(language)
//restarting app
val i = Intent(this, SplashScreenActivity::class.java)
startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK))
finish()
System.exit(0)
}
Run Code Online (Sandbox Code Playgroud)
class LocaleManager(val context: Context) {
val sharedPreferenceManager = createSharedPreferenceManager(context)
fun setLocale(): Context = updateResources()
fun setNewLocale(language: String): Context {
return updateResources(language)
}
private fun updateResources(l: String? = null): Context {
val language = l ?: sharedPreferenceManager.language
if (language.isBlank()) return context
val locale = Locale(language)
Locale.setDefault(locale)
val res = …Run Code Online (Sandbox Code Playgroud) 我正在实施生物识别技术,如果用户不想要/拥有生物识别技术,我希望能够回退到使用 PIN 码。
问题在于,当用户选择“使用 PIN”、输入他们的 PIN 并验证两个不同的回调时,会触发:
第一个:onAuthenticationSucceeded这是预期的。第二种:onAuthenticationError与BiometricConstants.ERROR_USER_CANCELED。
为什么输入 PIN 而不是生物识别会触发 ERROR_USER_CANCELED?我通过完成我的 Activity 处理了这个错误,这不是我想要的流程。我这样做是因为当用户点击对话框外时,我想关闭应用程序,并且在这种情况下会触发相同的错误(尽管有此修复)。
有没有办法区分有意取消对话(通过后退按钮或点击对话外)和输入 PIN 码?
这是我的PromptInfo:
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.lock_title))
.setSubtitle(getString(R.string.lock_summary))
.setConfirmationRequired(false)
.setDeviceCredentialAllowed(true)
.build()
Run Code Online (Sandbox Code Playgroud)
在装有 Android 10 的 Pixel 2 上进行测试。使用版本"androidx.biometric:biometric:1.0.1". 谢谢!
我目前正在为一个项目开发生物识别库,但似乎无法使面部识别工作。
BiometricManager 在我尝试过的所有设备中都可以正确识别指纹,但是它没有检测到我的华为 P30 ELE-L29(EMUI 版本 10.0.0 - Android 版本 10)的任何注册面部生物识别信息,并且当我调用 BiometricManager# 时返回 BIOMETRIC_ERROR_NONE_ENROLLED#可以验证()。这是我唯一可用的 API 版本 29 的设备,因为 Android Studio 模拟器没有实现面部识别,并且其他模拟器(Bluestacks、Nox 等)无法配置为在我的 MacBook 上的 Android 10 上运行。
我希望能对这个问题有所了解,因为面部识别对于原生 Android 来说是新的,而且我找不到任何类似的问题。我不确定这是否是这款特定手机的问题,或者我的代码是否有错误。
我的 minSdkVersion 是 23 (Android 6.0 Marshmallow),我的 targetSdkVersion 是 29 (Android 10 Q)。
我的(部分)实现:
fun isBiometricsSupported(@NonNull context: Context): Boolean {
val biometricManager = BiometricManager.from(context)
var isBiometricsSupported = false
when (biometricManager.canAuthenticate()) {
BiometricManager.BIOMETRIC_SUCCESS -> {
isBiometricsSupported = true
}
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
Log.println(Log.INFO, "a", "Logger: BIOMETRIC_ERROR_NO_HARDWARE")
}
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
Log.println(Log.INFO, …Run Code Online (Sandbox Code Playgroud)