Android 10 vivo设备调用Biometric API仅显示指纹无面部认证如何解决
问题根因
你遇到的问题是Android 10系统的Biometric API默认仅调用强生物认证(大部分厂商的Android 10系统仅将指纹归类为强生物认证,面部识别归类为弱生物认证),加上你代码中的配置冲突,导致面部识别入口被屏蔽。
解决步骤
- 修复
PromptInfo配置冲突
你当前代码同时调用了setNegativeButtonText()和setDeviceCredentialAllowed(true),这两个方法是互斥的,同时设置会导致setDeviceCredentialAllowed配置被系统忽略,进而触发默认仅支持强生物认证(指纹)的逻辑。如果需要保留设备凭据解锁能力,就删除setNegativeButtonText配置;如果不需要设备凭据,就删除setDeviceCredentialAllowed配置。 - 显式指定允许的生物认证等级
setDeviceCredentialAllowed在API 30及以上已废弃,推荐使用setAllowedAuthenticators显式指定支持的认证类型,兼容面部识别这类弱生物特征。注意要使用1.2.0以上版本的androidx.biometric库,保证适配Android 10的厂商实现。 - 补充必要权限
在AndroidManifest.xml中添加生物认证权限:
<uses-permission android:name="android.permission.USE_BIOMETRIC" android:required="false" /> <!-- 兼容更低版本可额外添加 --> <uses-permission android:name="android.permission.USE_FINGERPRINT" android:required="false" />
- 增加设备支持性校验
在触发认证前先调用BiometricManager.canAuthenticate()方法,校验当前设备是否支持你指定的认证类型,避免调用异常。 - vivo机型特殊适配
部分搭载Android 10的vivo设备,系统默认限制第三方应用调用面部识别能力,可进入系统设置的权限管理,找到你的应用,确认「生物识别」相关权限已开启。
修改后的核心代码示例
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_face_id) biometricPrompt = BiometricPrompt(this, ContextCompat.getMainExecutor(this), object : BiometricPrompt.AuthenticationCallback() { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { super.onAuthenticationError(errorCode, errString) Log.d("Huang", " error $errString") } override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { super.onAuthenticationSucceeded(result) Log.d("Huang", " success") } override fun onAuthenticationFailed() { super.onAuthenticationFailed() Log.d("Huang", " fail") } }) // 先校验认证可用性 val biometricManager = BiometricManager.from(this) // 指定允许弱生物认证(包含面部)+设备凭据 val authenticators = BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL val canAuth = biometricManager.canAuthenticate(authenticators) val button = findViewById<Button>(R.id.login) button.setOnClickListener { if (canAuth == BiometricManager.BIOMETRIC_SUCCESS) { val promptInfo = BiometricPrompt.PromptInfo.Builder() .setTitle("Biometric login for my app") .setSubtitle("Log in using your biometric credential") .setAllowedAuthenticators(authenticators) .build() biometricPrompt.authenticate(promptInfo) } else { // 不支持生物认证时走自定义密码登录逻辑 } } }
内容的提问来源于stack exchange,提问作者GHH
相关产品推荐
相关产品推荐

