Android指纹解锁方法解析
程序员文章站
2024-03-03 23:53:46
我先说说这两种的方式的不同之处吧
第一种:
在调动成功之后 不会让你指纹解锁 而是调转到当初你设置指纹解锁时的 手势解锁页面
第二种:
在调动成功之后,是进...
我先说说这两种的方式的不同之处吧
第一种:
在调动成功之后 不会让你指纹解锁 而是调转到当初你设置指纹解锁时的 手势解锁页面
第二种:
在调动成功之后,是进行指纹解锁 不调转 你直接把手指放到金属感应环 上面进行指纹验证
大家可以根据需求 自行选择
ok 那就亮代码了
第一种:
xml 布局: 一个 文本显示 一个按钮(不解释)
mainactivity.java源码
public class mainactivity extends fragmentactivity { fingerprintmanager manager; keyguardmanager mkeymanager; private final static int request_code_confirm_device_credentials = 0; private final static string tag = “finger_log”; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //1:先获取 指纹识别的的管理者 看看获取方式 是不是感觉就和数学公式一样 (xxx管理者)=this.getsystemservice(context.xxx服务) manager = (fingerprintmanager) this.getsystemservice(context.fingerprint_service); mkeymanager = (keyguardmanager) this.getsystemservice(context.keyguard_service); // 2 初始化按钮 设置监听 // 监听 监听 听什么 肯定是听听 这个手机有没有 指纹识别这个功能 所以 看看isfinger() button btn_finger = (button) findviewbyid(r.id.btn_activity_main_finger); btn_finger.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (isfinger()) { toast.maketext(mainactivity.this, "请进行指纹识别", toast.length_long).show(); log(tag, "keyi"); startlistening(null); } } }); } public boolean isfinger() { //android studio 上,没有这个会报错 if (activitycompat.checkselfpermission(this, manifest.permission.use_fingerprint) != packagemanager.permission_granted) { toast.maketext(this, "没有指纹识别权限", toast.length_short).show(); return false; } log(tag, "有指纹权限"); //判断硬件是否支持指纹识别 if (!manager.ishardwaredetected()) { toast.maketext(this, "没有指纹识别模块", toast.length_short).show(); return false; } log(tag, "有指纹模块"); //判断 是否开启锁屏密码 if (!mkeymanager.iskeyguardsecure()) { toast.maketext(this, "没有开启锁屏密码", toast.length_short).show(); return false; } log(tag, "已开启锁屏密码"); //判断是否有指纹录入 if (!manager.hasenrolledfingerprints()) { toast.maketext(this, "没有录入指纹", toast.length_short).show(); return false; } log(tag, "已录入指纹"); return true; } cancellationsignal mcancellationsignal = new cancellationsignal(); //回调方法 fingerprintmanager.authenticationcallback mselfcancelled = new fingerprintmanager.authenticationcallback() { @override public void onauthenticationerror(int errorcode, charsequence errstring) { //但多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证 toast.maketext(mainactivity.this, errstring, toast.length_short).show(); showauthenticationscreen(); } @override public void onauthenticationhelp(int helpcode, charsequence helpstring) { toast.maketext(mainactivity.this, helpstring, toast.length_short).show(); } @override public void onauthenticationsucceeded(fingerprintmanager.authenticationresult result) { toast.maketext(mainactivity.this, "指纹识别成功", toast.length_short).show(); } @override public void onauthenticationfailed() { toast.maketext(mainactivity.this, "指纹识别失败", toast.length_short).show(); } }; public void startlistening(fingerprintmanager.cryptoobject cryptoobject) { //android studio 上,没有这个会报错 if (activitycompat.checkselfpermission(this, manifest.permission.use_fingerprint) != packagemanager.permission_granted) { toast.maketext(this, "没有指纹识别权限", toast.length_short).show(); return; } manager.authenticate(cryptoobject, mcancellationsignal, 0, mselfcancelled, null); } /** * 锁屏密码 */ private void showauthenticationscreen() { intent intent = mkeymanager.createconfirmdevicecredentialintent("finger", "测试指纹识别"); if (intent != null) { startactivityforresult(intent, request_code_confirm_device_credentials); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_code_confirm_device_credentials) { // challenge completed, proceed with using cipher if (resultcode == result_ok) { toast.maketext(this, "识别成功", toast.length_short).show(); } else { toast.maketext(this, "识别失败", toast.length_short).show(); } } } private void log(string tag, string msg) { log.d(tag, msg); } }
===================================
第二种:
1:xml: 布局 一个文本提示 两个按钮(1: 开始识别 2:取消识别)
2: 咱们将业务操作放到一个fingerprintcontroller 类里面
private static fingerprintcontroller ssingleton = null; private context mcontext; private fingerprintmanagercompat manager; private static final string tag = "fingerprintcontroller"; private static final string premission = "android.permission.use_fingerprint"; //成功 public static final int finger_success = 0; //硬件不支持 public static final int finger_error_no_hardware = 1; //没有申请权限 public static final int finger_error_no_permission = 2; //用户没有赋予权限 //protection level: normal //指纹权限的级别是normal,理论上不需要动态权限认证 public static final int finger_error_no_user_permission = 3; //用户没有储存指纹 public static final int finger_error_no_finger = 4; //取消指纹识别 private cancellationsignal cancellationsignal; private fingerauthlistener mauthlistener; public static synchronized fingerprintcontroller getinstance(context context) { if (ssingleton == null) { ssingleton = new fingerprintcontroller(context); } return ssingleton; } public fingerprintcontroller(context context) { mcontext = context; manager = fingerprintmanagercompat.from(this.mcontext); } /** * 开始指纹识别 * 失败次数过多后需要一定时间后才可以重新启动 */ public void startfingerauth() { if (null == cancellationsignal) { cancellationsignal = new cancellationsignal(); } manager.authenticate(null, 0, cancellationsignal, new fingerauthcallback(), null); } /** * 取消指纹识别 */ public void cancelfingerauth() { if (cancellationsignal != null) { cancellationsignal.cancel(); if (mauthlistener != null) mauthlistener.cancel(); } } /** * 指纹识别回调 */ public class fingerauthcallback extends fingerprintmanagercompat.authenticationcallback { // 当出现错误的时候回调此函数,比如多次尝试都失败了的时候,errstring是错误信息 @override public void onauthenticationerror(int errmsgid, charsequence errstring) { // log.d(tag, “onauthenticationerror: ” + errstring); if (null != mauthlistener) mauthlistener.error(errstring.tostring()); } // 当指纹验证失败的时候会回调此函数,失败之后允许多次尝试,失败次数过多会停止响应一段时间然后再停止sensor的工作 @override public void onauthenticationfailed() { if (null != mauthlistener) mauthlistener.failure(); } @override public void onauthenticationhelp(int helpmsgid, charsequence helpstring) { if (null != mauthlistener) log.e("helpmsgid",helpmsgid+""); log.e("helpstring",helpstring.tostring()); mauthlistener.help(helpstring.tostring()); } // 当验证的指纹成功时会回调此函数,然后不再监听指纹sensor @override public void onauthenticationsucceeded(fingerprintmanagercompat.authenticationresult result) { if (null != mauthlistener) mauthlistener.success(); } } /** * 检查指纹解锁是否可用 * * @return 状态 */ public int checkfingerenable() { if (null == manager) { manager = fingerprintmanagercompat.from(mcontext); } if (!isapppermissionenable()) { return finger_error_no_permission; } if (!manager.ishardwaredetected()) { return finger_error_no_hardware; } if (!manager.hasenrolledfingerprints()) { return finger_error_no_finger; } if (!isuserpermissionenable()) { return finger_error_no_user_permission; } return finger_success; } /** * 是否声明了该权限 */ private boolean isapppermissionenable() { packagemanager pm = mcontext.getpackagemanager(); if (pm == null) { log.w(tag, "can't get packagemanager"); return true; } try { return packagemanager.permission_granted == pm.checkpermission(premission, mcontext.getpackagename()); } catch (exception e) { log.w(tag, "can't checkt permission " + e.getmessage()); return true; } } /** * 是否具有动态权限,理论上不需要验证 */ private boolean isuserpermissionenable() { if (build.version.sdk_int >= build.version_codes.m) { return packagemanager.permission_granted == mcontext.checkselfpermission(manifest.permission.use_fingerprint); } return true; } public void setauthlistener(fingerauthlistener authlistener) { mauthlistener = authlistener; } public interface fingerauthlistener { void success(); void error(string error); void help(string msg); void cancel(); void failure(); } }
3:在 main中 实现 fingerprintcontroller.fingerauthlistener这个 回调接口
private int code = fingerprintcontroller.finger_success; private textview toast; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toast = (textview) findviewbyid(r.id.toast); code = fingerprintcontroller.getinstance(this).checkfingerenable(); if (code == fingerprintcontroller.finger_success) { fingerprintcontroller.getinstance(this).setauthlistener(this); settoast("可以启动指纹识别"); } else { switch (code) { case fingerprintcontroller.finger_error_no_hardware: settoast("该设备不支持指纹识别"); break; case fingerprintcontroller.finger_error_no_permission: settoast("当前应用没有指纹识别权限"); break; case fingerprintcontroller.finger_error_no_finger: settoast("当前设备没有录入指纹,请前往录入指纹"); break; } } } public void start(view view) { if (code == fingerprintcontroller.finger_success) { fingerprintcontroller.getinstance(this).startfingerauth(); settoast("开始指纹识别"); } } public void cancel(view view) { if (code == fingerprintcontroller.finger_success) { fingerprintcontroller.getinstance(this).cancelfingerauth(); } } @override public void success() { settoast("识别成功"); } @override public void error(string error) { settoast(error); } @override public void help(string msg) { settoast(msg); } @override public void cancel() { settoast("取消指纹识别"); } @override public void failure() { settoast("指纹识别失败"); } public void settoast(string msg) { toast.settext("提示:" + msg); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。