指纹识别是在Android 6.0以后新增的功能,在使用的时候需要先判断手机的系统版本是否支持指纹识别。
AndroidManifest添加权限
<uses-permission android:name=".USE_FINGERPRINT" />
- 1
验证手机是否支持指纹功能
- FingerprintManager : 指纹管理类
public boolean supportFingerprint() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
FingerprintManager fingerprintManager = Application.getContext().getSystemService(FingerprintManager.class);
if (fingerprintManager != null && !fingerprintManager.isHardwareDetected()) {
return false;
}
} else {
return false;
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
根据系统版本 < 23, 提示用户手机系统版本过低,不支持指纹功能
() 是否有指纹硬件
() 是否有录入的指纹,提示用户跳转到系统设置录入指纹
指纹识别关键方法 authenticate
指纹识别中最关键的方法,调起指纹识别扫描器进行指纹识别
(CryptoObject crypto, CancellationSignal cancel, int flags, AuthenticationCallback callback, Handler handler);
FingerprintManager.authenticate(null, null, 0, new FingerAuthenticateCallBack(), null);
- 1
指纹验证结果回调
public class FingerAuthenticateCallBack extends FingerprintManager.AuthenticationCallback{
private String TAG = "FingerAuthenticateCallBack";
// 指纹验证多次错误的时候回调方法,多次尝试都失败了的时候,errString为错误信息
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
Log.d(TAG, "onAuthenticationError: " + errString);
}
// 当指纹验证失败的时候会回调此方法
@Override
public void onAuthenticationFailed() {
Log.d(TAG, "onAuthenticationFailed: 验证失败");
}
// 在认证期间遇到可恢复的错误时调用,比如手指移到太快指纹传感器可能只采集了部分的信息
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Log.d(TAG, "onAuthenticationHelp: " + helpString);
}
// 当验证的指纹成功时会回调此函数
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Log.d(TAG, "onAuthenticationSucceeded: " + "验证成功");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
2020-08-06 16:43:04.204 783-783 D/FingerAuthenticateCallBack: onAuthenticationFailed: 验证失败
2020-08-06 16:43:10.113 783-783 D/FingerAuthenticateCallBack: onAuthenticationHelp: 手指移动太快,请重试。
2020-08-06 16:43:13.464 783-783 D/FingerAuthenticateCallBack: onAuthenticationFailed: 验证失败
2020-08-06 16:43:15.626 783-783 D/FingerAuthenticateCallBack: onAuthenticationSucceeded: 验证成功
FingerprintManager虽然标为废弃,但还是可以正常使用的。
BiometricPrompt 生物识别
在AndroidP(9.0)时候,官方不再推荐使用FingerprintManager,标记为@Deprecated,开放BiometricPrompt新的API
AndroidManifest添加权限
<uses-permission android:name=".USE_BIOMETRIC" />
- 1
指纹识别关键方法 authenticate
(CancellationSignal cancel,CallbackExecutor Executor executor,AuthenticationCallback callback)
用于调起指纹识别扫描器进行指纹识别
BiometricPrompt biometricPrompt = new BiometricPrompt.Builder(this)
.setTitle("指纹验证")
.setNegativeButton("使用密码验证", getMainExecutor(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("TAG", "cancel");
}
})
.build();
BiometricPrompt.AuthenticationCallback authenticationCallback = new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Log.d("TAG", "onAuthenticationError errorCode: " + errorCode + " errString: " + errString);
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
Log.d("TAG", "onAuthenticationHelp helpCode:" + helpCode + "helpString: " + helpString);
}
@Override
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Log.d("TAG", "验证成功");
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Log.d("TAG", "onAuthenticationFailed");
}
};
biometricPrompt.authenticate(new CancellationSignal(), getMainExecutor(), authenticationCallback);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
CancellationSignal用来在指纹识别器扫描用户指纹的是时候取消当前的扫描操作,如果不取消的话,指纹扫描器会一直扫描直到超时同时也会比较耗电。
()方法用于取消指纹识别器扫描
Android P(9.0) 系统提供指纹识别框
相比Android 6.0 指纹识别框可以自定义,而9.0不允许开发者自定义指纹识别框
前面调起指纹识别扫描器的各个回调方法介绍过了
()其实跟()基本类似
也是最近需要用到指纹识别的功能,才了解的。
希望对有需要的小伙伴有帮助~