You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android M(6)至Pie(9)版本:Fingerprint Manager与Biometrics选型咨询

方案选择与实现指南

嗨,结合你的项目配置(minSdk 23,compileSdk 28,覆盖Android 6到9),我强烈建议你使用Biometrics API,而不是已经被弃用的FingerprintManager,下面给你详细拆解原因和实现步骤:

为什么选Biometrics?

  • FingerprintManager从Android 9(API28)开始就被官方标记为弃用,后续系统版本会逐步缩减对它的支持,从长期维护角度看Biometrics是更稳妥的选择。
  • Biometrics API(核心是BiometricPrompt)不仅支持指纹,还预留了对其他生物识别方式(比如面部识别)的扩展空间,比单一的指纹API更具前瞻性。
  • 通过AndroidX的兼容库,Biometrics可以完美覆盖你的目标版本(Android 6到9),不需要自己写复杂的版本分支逻辑。

Biometrics对旧版本的兼容性

完全兼容!只要你使用AndroidX提供的biometric库,它会在底层自动适配不同API版本:

  • 对于Android 6-8(API23-27),库会内部调用FingerprintManager的逻辑实现指纹认证;
  • 对于Android 9(API28),则直接调用系统原生的BiometricPrompt API。

基于Biometrics的编码步骤

1. 添加依赖

在你的build.gradle(Module level)中添加AndroidX生物识别库的依赖:

dependencies {
    implementation 'androidx.biometric:biometric:1.1.0' // 可替换为最新稳定版
}

2. 检查设备生物识别支持情况

启动认证前,先通过BiometricManager判断设备是否具备可用的生物识别能力:

val biometricManager = BiometricManager.from(context)
when (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK)) {
    BiometricManager.BIOMETRIC_SUCCESS -> {
        // 设备支持生物识别,可启动认证流程
    }
    BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
        // 设备无生物识别硬件
    }
    BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
        // 生物识别硬件当前不可用
    }
    BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
        // 用户未录入任何生物识别信息,引导前往系统设置录入
    }
}

注:BIOMETRIC_WEAK对应指纹这类相对弱的生物识别方式,完全符合你的需求;若需要更强验证,可使用BIOMETRIC_STRONG(Android 9之前基本只有指纹属于此范畴)。

3. 创建BiometricPrompt并设置回调

val executor = ContextCompat.getMainExecutor(context)
val biometricPrompt = BiometricPrompt(
    this, // 传入Activity/Fragment实例
    executor,
    object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            // 认证出错处理:用户多次失败、主动取消等
            Toast.makeText(context, "认证出错:$errString", Toast.LENGTH_SHORT).show()
        }

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            // 认证成功,处理聊天应用解锁逻辑
            Toast.makeText(context, "指纹认证成功!", Toast.LENGTH_SHORT).show()
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            // 认证失败:指纹不匹配等情况
            Toast.makeText(context, "指纹不匹配,请重试", Toast.LENGTH_SHORT).show()
        }
    }
)

4. 构建认证弹窗配置

val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("解锁聊天应用")
    .setSubtitle("请验证你的指纹")
    .setDescription("验证通过后即可访问聊天内容")
    .setNegativeButtonText("取消") // 系统默认取消按钮,也可改为"使用密码"引导设备凭证验证
    // 若允许用户用PIN/图案/密码替代指纹,添加此行
    // .setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)
    .build()

5. 启动认证流程

最后调用authenticate方法弹出认证弹窗:

biometricPrompt.authenticate(promptInfo)

额外注意事项

  • 在AndroidManifest.xml中添加必要权限:
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
  • Android 6(API23)不需要动态申请该权限,因为USE_BIOMETRIC属于正常权限,系统会自动授予。

这样一套实现下来,既能利用新API的优势,又能完美覆盖你需要的Android版本区间,代码简洁且易维护。

内容的提问来源于stack exchange,提问作者Shashank Verma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:30:31