如何使用androidx BiometricPrompt实现指纹验证功能

发布时间:2021-07-30 14:37:43 作者:chen
来源:亿速云 阅读:699

本篇内容介绍了“如何使用androidx BiometricPrompt实现指纹验证功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

androidsdk版本大于29之后,使用FingerprintManagerCompat进行指纹验证显示被废弃,FingerprintManagerCompat的使用方法这里不再叙述。骨骼要求使用新的api去完成指纹验证,当然,BiometricPrompt不仅能做指纹验证,本文只讲解怎么用BiometricPrompt做指纹验证。
官方api:https://developer.android.google.cn/reference/androidx/biometric/package-summary?hl=zh-cn

首先导包

 implementation 'androidx.biometric:biometric:1.0.1'

然后它的构造方法

1.BiometricPrompt(@NonNull FragmentActivity fragmentActivity,
        @NonNull Executor executor, @NonNull AuthenticationCallback callback)
  2. BiometricPrompt(@NonNull Fragment fragment,
        @NonNull Executor executor, @NonNull AuthenticationCallback callback)

两个构造方法参数基本一致,executor里面是一个runnable接口,在每次进行指纹操作后都会回调这个方法,注意:要想AuthenticationCallback的方法生效,必须在runnable里面执行runnable的run方法。
callback里面有三个回调方法,
1. onAuthenticationError(int errMsgId, CharSequence errString),指纹验证错误会调用此方法,errMsgId的值对应BiometricPrompt里面的常量
2. onAuthenticationSucceeded(@NonNull @NotNull BiometricPrompt.AuthenticationResult result),指纹验证成功后调用,通过result.getAuthenticationType获取验证成功的方式,参数类型自行查看。
3. onAuthenticationFailed() 识别失败调用,具体调用时机不太清楚。。可以参考官方文档说法

显示指纹验证需要一个BiometricPrompt.PromptInfo参数,会弹起一个弹窗进行显示,使用builder的方式初始化,可以设置title,subTitle,description,NegativeButtonText,用法如下

 new BiometricPrompt.PromptInfo.Builder().setTitle("title")
   .setSubtitle("subTitle")
   .setDescription("description")
   .setDeviceCredentialAllowed(false)
   .setNegativeButtonText("button").build()

需要注意的是setDeviceCredentialAllowed与setNegativeButtonText只能存在一个,即setNegativeButtonText不为空setDeviceCredentialAllowed必须为false
验证设备是否开启指纹通过BiometricManager.from(context).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS方法;
代码展示:

private BiometricPrompt biometricPrompt;
       private void startFinger(){
       			 biometricPrompt = new BiometricPrompt(this, new Executor() {
              			  @Override
              			  public void execute(Runnable command) {
                 			   command.run();
            			    }
         			   }, new FingerCallBack());
     			  biometricPrompt.authenticate( new BiometricPrompt.PromptInfo.Builder().setTitle("title")
      			 .setSubtitle("subTitle")
       			 .setDescription("description")
       			 .setDeviceCredentialAllowed(false)
      			 .setNegativeButtonText("button").build());
       }
          private void cancelFinger() {
      			  if (biometricPrompt != null) {
          			  biometricPrompt.cancelAuthentication();
     			   }
    }

private class FingerCallBack extends BiometricPrompt.AuthenticationCallback {
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            super.onAuthenticationError(errMsgId, errString);
            Log.e("fingers", "onAuthenticationError");
        }
        @Override
        public void onAuthenticationSucceeded(@NonNull @NotNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            cancelFinger();
            Log.e("fingers", "识别成功 onAuthenticationSucceeded");
        }
        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Log.e("fingers", "onAuthenticationFailed  ");
        }
    }

“如何使用androidx BiometricPrompt实现指纹验证功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. SSH-key详解及其在Git中的使用
  2. 如何使用python实现字符串加密生成唯一固定长度字符串

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

androidx

上一篇:anaconda中怎么将conda 切换为清华源

下一篇:Spring boot中怎么实现远程更新

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》