目录
写作不易,转载请注明出处:
https://blog.csdn.net/qq_34676644/article/details/118758483
or
https://www.jianshu.com/p/530c9fa31786
前言
官方的指纹识别在Android 6.0引入,Android 6.0之前指纹识别由厂商自己定义。所以Android 6.0之前的指纹识别碎片化严重。
概览
1.类
1. FingerprintManager
:Android 6.0引入,Android 9.0 废弃。使用时需加入权限:permission USE_FINGERPRINT
注:包含检查是否支持指纹,指纹比对是否成功等。支持包中的
FingerprintManagerCompat
类对其作了包装和兼容处理。不做详细介绍
2.BiometricManager
:Android 9.0引入。其不仅包含指纹识别,还包含人脸识别等其他验证方式。
其主要功能为:检查当前设备是否具有指纹识别的条件,例如:设备是否具有指纹识别的硬件,指纹是否已经录入,硬件是否可用等。
需要配合权限permission USE_BIOMETRIC
使用
注:目前,该类只有指纹识别的相关API,以后可能会加入人脸识别等其他生物识别的相关API。
推荐使用AndroidX支持库中的类,其已经对Android 6.0-Android 11做了兼容性处理:
- 在Android 9 版本和之前版本中会调用
FingerprintManagerCompat
- 在Android 10 版本开始会调用框架层
BiometricManager
3.BiometricPrompt
:
其主要功能为:发起指纹验证
需要配合权限permission USE_BIOMETRIC
使用
4.BiometricPrompt.PromptInfo
:
其主要功能为:验证对话框
通过其内部类
BiometricPrompt.PromptInfo.Builder
,采用创造者模式配置对话框。仅能配置少量参数,例如:标题,副标题等。这意味着:
1.不分Android版本,对话框的样式统一
2.无法自定义对话款样式
2.权限
permission USE_FINGERPRINT
:使用FingerprintManager
及FingerprintManagerCompat
类进行指纹识别时,需获取此权限。此权限为:Normal Permission
permission USE_BIOMETRIC
:使用BiometricManager
等类进行指纹识别时,需获取此权限。当使用AndroidX支持库中的
BiometricManager
时候不需要声明此权限,因为支持库中已经添加此权限
详细介绍
1.BiometricManager
属性和方法
属性 | 含义 |
---|---|
BIOMETRIC_ERROR_HW_UNAVAILABLE (value:1) |
The user can’t authenticate because the hardware is unavailable. Try again later (传感器当前不可用,清稍后再试) |
BIOMETRIC_ERROR_NONE_ENROLLED (value:11) |
The user can’t authenticate because no biometric or device credential is enrolled.(信息没有录入,比如还没录入指纹) |
BIOMETRIC_ERROR_NO_HARDWARE (value:12) |
The user can’t authenticate because there is no suitable hardware (e.g. no biometric sensor or no keyguard).(没有合适的传感器或者没设置密码,例如手机没有指纹传感器) |
BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED (value:15) |
The user can’t authenticate because a security vulnerability has been discovered with one or more hardware sensors. The affected sensor(s) are unavailable until a security update has addressed the issue.(传感器存在已知的漏洞,在更新修复漏洞前,传感器不可用) |
BIOMETRIC_ERROR_UNSUPPORTED (value:-2) |
The user can’t authenticate because the specified options are incompatible with the current Android version.(设置的一些验证条件,当前手机的Android版本无法满足) |
BIOMETRIC_STATUS_UNKNOWN (value:-1) |
Unable to determine whether the user can authenticate(不知道是否可以进行验证。通常在旧版本的Android手机上出现,当出现这个错误是,仍然可以尝试进行验证) |
BIOMETRIC_SUCCESS (value:0) |
The user can successfully authenticate.(可以进行验证) |
方法名 | 作用 | 返回值 |
---|---|---|
canAuthenticate() (已废弃)推荐使用canAuthenticate(int) |
检查传感器是否可用。 | 是否可用的状态码 |
canAuthenticate (int authenticators) |
检查传感器是否可用。 | 是否可用的状态码 |
from(Context context) (静态方法) |
创建BiometricManager |