Android6.0指纹识别开发实例详解

Android6.0指纹识别开发实例详解,第1张

概述Android6.0指纹识别开发实例详解最近在做android指纹相关的功能,谷歌在android6.0及以上版本对指纹识别进行了官方支持。当时在FingerprintManager和FingerprintManagerCompat这两个之间纠结,其中使用FingerprintM

AndroID6.0指纹识别开发实例详解

最近在做androID指纹相关的功能,谷歌在androID6.0及以上版本对指纹识别进行了官方支持。当时在FingerprintManager和FingerprintManagerCompat这两个之间纠结,其中使用FingerprintManager要引入com.androID.support:appcompat-v7包,考虑到包的大小,决定使用v4兼容包FingerprintManagerCompat来实现。

主要实现的工具类FingerprintUtil:验证手机是否支持指纹识别方法callFingerPrintVerify(),主要验证手机硬件是否支持(6.0及以上),有没有录入指纹,然后有没有开启锁屏密码,开始验证对于识别成功,失败可以进行相应的回调处理。

实例代码:

 public class FingerprintUtil{ private FingerprintManagerCompat mFingerprintManager; private KeyguardManager mKeyManager; private CancellationSignal mCancellationSignal; private Activity mActivity; public FingerprintUtil(Context ctx) {  mActivity = (Activity) ctx;  mFingerprintManager = FingerprintManagerCompat.from(mActivity);  mKeyManager = (KeyguardManager) mActivity.getSystemService(Context.KEyguard_SERVICE); } public voID callFingerPrintVerify(final IFingerprintResultListener Listener) {  if (!isHarDWareDetected()) {   return;  }  if (!isHasEnrolledFingerprints()) {   if (Listener != null) {    Listener.onNoEnroll();   }   return;  }  if (!isKeyguardSecure()) {   if (Listener != null) {    Listener.onInSecurity();   }   return;  }  if (Listener != null) {   Listener.onSupport();  }  if (Listener != null) {   Listener.onAuthenticateStart();  }  if (mCancellationSignal == null) {   mCancellationSignal = new CancellationSignal();  }  try {   mFingerprintManager.authenticate(null,mCancellationSignal,new FingerprintManagerCompat.AuthenticationCallback() {    //多次尝试都失败会走onAuthenticationError,会停止响应一段时间,提示尝试次数过多,请稍后再试。    @OverrIDe    public voID onAuthenticationError(int errMsgid,CharSequence errString) {     if (Listener != null)      Listener.onAuthenticateError(errMsgid,errString);    }    //指纹验证失败走此方法,例如小米前4次验证失败走onAuthenticationFailed,第5次走onAuthenticationError    @OverrIDe    public voID onAuthenticationFailed() {     if (Listener != null)      Listener.onAuthenticateFailed();    }    @OverrIDe    public voID onAuthenticationHelp(int helpMsgid,CharSequence helpString) {     if (Listener != null)      Listener.onAuthenticateHelp(helpMsgid,helpString);    }    //当验证的指纹成功时会回调此函数,然后不再监听指纹sensor    @OverrIDe    public voID onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {     if (Listener != null)      Listener.onAuthenticateSucceeded(result);    }   },null);  } catch (Exception e) {   e.printstacktrace();  } } /**  * 是否录入指纹,有些设备上即使录入了指纹,但是没有开启锁屏密码的话此方法还是返回false  *  * @return  */ private boolean isHasEnrolledFingerprints() {  try {   return mFingerprintManager.hasEnrolledFingerprints();  } catch (Exception e) {   return false;  } } /**  * 是否有指纹识别硬件支持  *  * @return  */ public boolean isHarDWareDetected() {  try {   return mFingerprintManager.isHarDWareDetected();  } catch (Exception e) {   return false;  } } /**  * 判断是否开启锁屏密码  *  * @return  */ private boolean isKeyguardSecure() {  try {   return mKeyManager.isKeyguardSecure();  } catch (Exception e) {   return false;  } } /**  * 指纹识别回调接口  */ public interface IFingerprintResultListener {  voID onInSecurity();  voID onNoEnroll();  voID onSupport();  voID onAuthenticateStart();  voID onAuthenticateError(int errMsgid,CharSequence errString);  voID onAuthenticateFailed();  voID onAuthenticateHelp(int helpMsgid,CharSequence helpString);  voID onAuthenticateSucceeded(FingerprintManagerCompat.AuthenticationResult result); } public voID cancelAuthenticate() {  if (mCancellationSignal != null) {   mCancellationSignal.cancel();   mCancellationSignal = null;  } } public voID onDestroy() {  cancelAuthenticate();  mKeyManager = null;  mFingerprintManager = null; }

参考了一些资料,做了一些验证,得到一些结论:

1、当指纹识别失败后,会调用onAuthenticationFailed()方法,这时候指纹传感器并没有关闭,谷歌原生系统给了我们5次重试机会,也就是说,连续调用了4次onAuthenticationFailed()方法后,第5次会调用onAuthenticateError(int errMsgid,CharSequence errString)方法,此时errMsgid==7。

2、每次重新授权,哪怕不去校验,取消时会走onAuthenticateError(int errMsgid,CharSequence errString) 方法,其中errMsgid==5,

3、当系统调用了onAuthenticationError()和onAuthenticationSucceeded()后,传感器会关闭,只有我们重新授权,再次调用authenticate()方法后才能继续使用指纹识别功能。

4、兼容androID6.0以下系统的话,不要使用FingerprintManagerCompat, 低于M的系统版本,FingerprintManagerCompat无论手机是否有指纹识别模块,均认为没有指纹识别,可以用FingerprintManager来做。

5、考虑到安全因素,最好authenticate(CryptoObject crypto,CancellationSignal cancel,int flags,AuthenticationCallback callback,Handler handler)时加入CryptoObject 。crypto这是一个加密类的对象,指纹扫描器会使用这个对象来判断认证结果的合法性。这个对象可以是null,但是这样的话,就意味着app无条件信任认证的结果,这个过程可能被攻击,数据可以被篡改,这是app在这种情况下必须承担的风险。因此,建议这个参数不要置为null。这个类的实例化有点麻烦,主要使用javax的security接口实现。

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的Android6.0指纹识别开发实例详解全部内容,希望文章能够帮你解决Android6.0指纹识别开发实例详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146370.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存