最近做了一个功能,需要添加指纹识别;我们都知道指纹识别接口是Google在6.0及以后才提供了相关的接口,所以今天我们讨论的都是基于Android API开发版本大于等于23(6.0)且硬件支持的机型。其他不做讨论。
下面就来看看实现指纹识别究竟有多么简单,首先我先说一下实现方式,第一种是通过FingerPrintManager方式,还有一种是通过v4包的FingerPrintManagerCompat方式。下面就来看看实现流程:
(1)方式1
- 第一步:获取FingerPrintManager实例
FingerprintManager manager;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager = this.getSystemService(FingerprintManager.class);//方式1
//manager=(FingerprintManager)this.getSystemService(Context.FINGERPRINT_SERVICE);//方式2
}
- 第二步:开启指纹识别并添加识别结果回调接口
manager.authenticate(null, null, 0, new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Log.e(TAG, "FingerprintManager: 指纹识别成功");
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Log.e(TAG, "FingerprintManager: 指纹识别失败");
}
}, null);