Android指纹登录工具类封装
程序员文章站
2022-03-23 22:00:26
本文实例为大家分享了android指纹登录工具类的封装代码,供大家参考,具体内容如下核心android 指纹 是在 6.0 才出来的一个重要的功能@requiresapi(api = build.ve...
本文实例为大家分享了android指纹登录工具类的封装代码,供大家参考,具体内容如下
核心
android 指纹 是在 6.0 才出来的一个重要的功能
@requiresapi(api = build.version_codes.m)
核心的两个 api:
fingerprintmanager
keyguardmanager
step1
判断android 版本,如果小于 6.0 支持不了指纹
if(build.version.sdk_int < build.version_codes.m){ return; }
step2
判断 手机硬件(有没有指纹感应区)就是手机是否支持传感
@requiresapi(api = build.version_codes.m) public boolean ishardfinger() { if (fingerprintmanager != null && fingerprintmanager.ishardwaredetected()) { return true; } else { return false; } }
step3
检查手机是否开启锁屏密码(如手机未开锁,涉及到一个优先级问题,先解锁 后使用)
public boolean iswindowsafe() { if (keyguardmanager != null && keyguardmanager.iskeyguardsecure()) { return true; } else { return false; } }
step4
检查手机是否有录入指纹
@requiresapi(api = build.version_codes.m) public boolean ishavehandler() { if (fingerprintmanager != null && fingerprintmanager.hasenrolledfingerprints()) { return true; } else { return false; } }
只有以上步骤全满足,才能使用指纹
开启指纹验证
@requiresapi(api = build.version_codes.m) public void authenticate(fingerprintmanager.cryptoobject cryptoobject, cancellationsignal cancellationsignal, int flag, fingerprintmanager.authenticationcallback authenticationcallback, handler handler) { if (fingerprintmanager != null) { fingerprintmanager.authenticate(cryptoobject, cancellationsignal, flag, authenticationcallback, handler); } }
参数中最重要的就是 cancellationsignal和 callback,其他传null 和 0 就行,cancellationsignal 是用来取消指纹验证的,而callback 可以回调 指纹验证失败次数 或者指纹验证成功
最后附上简单工具类
/** * 指纹识别工具类 */ public class fingerutils { private final fingerprintmanager fingerprintmanager; private final keyguardmanager keyguardmanager; @requiresapi(api = build.version_codes.m) private fingerutils(context context) { fingerprintmanager = (fingerprintmanager) context.getsystemservice(context.fingerprint_service); keyguardmanager = (keyguardmanager) context.getsystemservice(context.keyguard_service); } private static fingerutils singleton = null; @requiresapi(api = build.version_codes.m) public static fingerutils getinstance(context context) { if (singleton == null) { synchronized (fingerutils.class) { if (singleton == null) { singleton = new fingerutils(context); } } } return singleton; } /** * ②检查手机硬件(有没有指纹感应区) */ @requiresapi(api = build.version_codes.m) public boolean ishardfinger() { if (fingerprintmanager != null && fingerprintmanager.ishardwaredetected()) { return true; } else { return false; } } /** * ③检查手机是否开启锁屏密码 */ public boolean iswindowsafe() { if (keyguardmanager != null && keyguardmanager.iskeyguardsecure()) { return true; } else { return false; } } /** * ④检查手机是否已录入指纹 */ @requiresapi(api = build.version_codes.m) public boolean ishavehandler() { if (fingerprintmanager != null && fingerprintmanager.hasenrolledfingerprints()) { return true; } else { return false; } } /** * 创建指纹验证 */ @requiresapi(api = build.version_codes.m) public void authenticate(fingerprintmanager.cryptoobject cryptoobject, cancellationsignal cancellationsignal, int flag, fingerprintmanager.authenticationcallback authenticationcallback, handler handler) { if (fingerprintmanager != null) { fingerprintmanager.authenticate(cryptoobject, cancellationsignal, flag, authenticationcallback, handler); } } /** * 取消指纹验证 . 应该不会用上 */ public void cannelfinger(cancellationsignal cancellationsignal) { cancellationsignal.cancel(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: openssl ca activemq
下一篇: Spark pairRDD