搭建直播带货平台,androidx指纹验证
搭建直播带货平台,androidx指纹验证实现的相关代码
androidsdk版本大于29之后,使用FingerprintManagerCompat进行指纹验证显示被废弃,FingerprintManagerCompat的使用方法这里不再叙述。骨骼要求使用新的api去完成指纹验证,当然,BiometricPrompt不仅能做指纹验证,本文只讲解怎么用BiometricPrompt做指纹验证。
官方api:https://developer.android.google.cn/reference/androidx/biometric/package-summary?hl=zh-cn
首先导包
```cpp implementation 'androidx.biometric:biometric:1.0.1' ```
然后它的构造方法
```cpp 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,用法如下
```cpp 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方法;
代码展示:
```cpp 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指纹验证实现的相关代码, 更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现