Android 6.0使用指纹识别
2018-01-17 17:21
288 查看
Android6.0起Google添加了官方的指纹识别api,可以使第三方应用方便的调用指纹。FingerprintManager 通过getSystemService(Context.FINGERPRINT_SERVICE)来获得,主要有以下三个方法
isHardwareDetected()用来判断设备是否存在指纹模块,hasEnrolledFingerprints()判断用户是否录入了指纹,在使用指纹前需要先判断这两个。authenticate就是真正调用指纹识别的地方下面说说传人的参数CryptoObject:调用指纹的时候不需要这个字段,Null就可以CancellationSignal:每次申请指纹识别时需要如果原来的已经被取消,需要再重新new一个,可以调用cancel()方法实现主动取消识别,主要的作用就是取消识别flags:只能为0Handler:指定线程,可以为空,为空时会自动生成一个主线程的Handler.AuthenticationCallback:是最重要的,所有的回调都在这里处理,具体使用可以参考源码注释。
Public Methods | |
---|---|
void | authenticate(FingerprintManager.CryptoObject crypto, CancellationSignal cancel,int flags, FingerprintManager.AuthenticationCallback callback, Handler handler)Request authentication of a crypto object. |
boolean | hasEnrolledFingerprints()Determine if there is at least one fingerprint enrolled. |
boolean | isHardwareDetected()Determine if fingerprint hardware is present and functional. |
public static abstract class AuthenticationCallback { /** * Called when an unrecoverable error has been encountered and the operation is complete. * No further callbacks will be made on this object. * @param errorCode An integer identifying the error message * @param errString A human-readable error string that can be shown in UI */ public void onAuthenticationError(int errorCode, CharSequence errString) { } /** * Called when a recoverable error has been encountered during authentication. The help * string is provided to give the user guidance for what went wrong, such as * "Sensor dirty, please clean it." * @param helpCode An integer identifying the error message * @param helpString A human-readable string that can be shown in UI */ public void onAuthenticationHelp(int helpCode, CharSequence helpString) { } /** * Called when a fingerprint is recognized. * @param result An object containing authentication-related data */ public void onAuthenticationSucceeded(AuthenticationResult result) { } /** * Called when a fingerprint is valid but not recognized. */ public void onAuthenticationFailed() { } /** * Called when a fingerprint image has been acquired, but wasn't processed yet. * * @param acquireInfo one of FINGERPRINT_ACQUIRED_* constants * @hide */ public void onAuthenticationAcquired(int acquireInfo) {} };需要注意的是一般都会提供几次验证的机会,而不是一次验证失败就进行下一步,我们的处理应该是在onAuthenticationError而不是onAuthenticationFailed中onAuthenticationError中的errorCode值的含义在FingerprintManager的静态变量中,注意需要区分的是用户主动取消指纹识别即调用CancellationSignal的cancel方法(返回错误码5)和真正的失败错误(其它错误码)。
为了方便开发者开发,google在v4兼容库中加入了指纹识别(FingerprintManagerCompat.class),
public static FingerprintManagerCompat from(Context context) { return new FingerprintManagerCompat(context); }
只添加了一个方法,其它的和之前一致
static { final int version = Build.VERSION.SDK_INT; if (version >= 23) { IMPL = new Api23FingerprintManagerCompatImpl(); } else { IMPL = new LegacyFingerprintManagerCompatImpl(); } }
private static class LegacyFingerprintManagerCompatImpl implements FingerprintManagerCompatImpl { public LegacyFingerprintManagerCompatImpl() { } @Override public boolean hasEnrolledFingerprints(Context context) { return false; } @Override public boolean isHardwareDetected(Context context) { return false; } @Override public void authenticate(Context context, CryptoObject crypto, int flags, CancellationSignal cancel, AuthenticationCallback callback, Handler handler) { // TODO: Figure out behavior when there is no fingerprint hardware available } }
自动判断了android版本消息并进行不同的处理,但实际上还是只支持6.0机型,6.0以下的直接不能使用
由于Google是在6.0才引入指纹识别,而很多厂家在之前就加入了指纹,适配就成为了问题,有以下的解决方案:
1、只识别6.0及以上机型,直接调用官方文档就可以,也要注意个别机型会出现调用异常,需要单独处理
2、适配官方api接口的机型,只需要去掉6.0版本的判断,再在调用方法的时候catch,天知道会出现什么错误。
3、适配所有机型,这就需要使用厂家的接口文档来做不同的处理了。
以上,就说明了下官方的使用方法,Demo就自己写了,很简单的。
相关文章推荐
- Android 6.0指纹识别App开发demo
- Android 6.0指纹识别相关API
- Android 6.0指纹识别App开发Demo
- Android 6.0指纹识别App开发demo
- 使用Runtime类 识别 sdcard和U盘android 6.0 存储路径识别
- Android开发学习—指纹识别系统的原理与使用
- Android指纹识别深入浅出分析到实战(6.0以下系统适配方案)
- Android 6.0指纹识别App开发案例
- Android指纹识别深入浅出分析到实战(6.0以下系统适配方案)
- Android 6.0 指纹识别功能详细分析(郭元歆)
- Android指纹识别认识和基本使用详解
- Android 6.0 指纹识别功能学习(一)----阿冬专栏!!!
- Android 6.0指纹识别App开发demo
- Android 6.0 指纹识别功能详细分析
- 关于android指纹识别兼容6.0以下版本
- Android 6.0指纹识别App开发demo
- Android 6.0+指纹识别心得
- Android指纹识别功能深入浅出分析到实战(6.0以下系统解决方案)
- android 6.0 指纹识别调用 Demo
- android语音识别方法一:使用intent调用语音识别程序