android实现实名认证
Many applications on your device store some type of private data that is to be protected by another unauthorized user (e.g. banking apps, finance apps, etc.). The best way to protect this type of data is through biometric authentication in the app. With a biometric lock, users who don’t have access to your password can’t open the app without the owner’s permission.
设备上的许多应用程序存储某种类型的私有数据,这些数据将由另一位未经授权的用户(例如,银行应用程序,金融应用程序等)保护。 保护此类数据的最佳方法是通过应用程序中的生物特征认证。 使用生物特征识别锁定后,没有所有者密码的用户将无法打开应用程序。
When I first heard about the concept of a biometric lock, I thought it would be hard to implement. In actuality, it was easy.
当我第一次听说生物识别锁的概念时,我认为很难实现。 实际上,这很容易。
Let’s go through a short demo for biometric authentication in Android.
让我们看一下简短的演示,以了解Android中的生物特征认证。
实作 (Implementation)
- Create a new project in Android Studio. 在Android Studio中创建一个新项目。
Add a dependency in the
app build.gradle
file.在
app build.gradle
文件中添加一个依赖app build.gradle
。- Add a button and text to your Main Activity. 在您的主要活动中添加按钮和文字。
Create the
BiometricPrompt
object in theMainActivity.kt
file.在
MainActivity.kt
文件中创建BiometricPrompt
对象。Now we will make an object in
Oncreate()
and a boolean variable namedisCapable
that will return whether the device has biometric authentication or not.现在,我们将在
Oncreate()
创建一个对象,并创建一个名为isCapable
的布尔变量,该布尔变量将返回设备是否具有生物识别身份验证。Using the
BiometricPrompt
object, add callbacks for authentication errors, successes, and failures.使用
BiometricPrompt
对象,添加用于身份验证错误,成功和失败的回调。
There are mainly three callbacks for biometric authentication:
生物识别身份验证主要有三个回调:
onAuthenticationError
onAuthenticationError
onAuthenticationSucceeded
onAuthenticationSucceeded
onAuthenticationFailed
onAuthenticationFailed
There are many possible reasons for an authentication error (e.g. the password is incorrect, something went wrong with the biometric lock, etc.). The same goes for authentication success and authentication failure. There are many possibilities.
身份验证错误的原因可能很多(例如密码不正确,生物特征识别锁出了毛病等)。 身份验证成功和身份验证失败也是如此。 有很多可能性。
You can use a unique code to show an error or any message as per the app requirements.
您可以根据应用要求使用唯一的代码显示错误或任何消息。
You can find all the code and callback types in Android’s documentation.
您可以在Android文档中找到所有代码和回调类型。
7. Add the biometric PromptInfo
object.
7.添加生物PromptInfo
对象。
You can edit
setTitle
andsetSubtitle
as per the message you want on the lock screen.您可以根据需要在锁定屏幕上显示的消息来编辑
setTitle
和setSubtitle
。setDeviceCredentialAllowed
will allow the user to use options aside from biometric authentication to unlock the app (e.g. password, number password, or pattern lock).setDeviceCredentialAllowed
将允许用户使用生物识别身份验证以外的选项来解锁应用程序(例如密码,数字密码或图案锁定)。setConfirmationRequired
will confirm the user credentials given, but only when the face lock is active.setConfirmationRequired
将确认给定的用户凭据,但仅当面部锁定处于活动状态时。
8. The final step is the button click event on which the app lock’s dialog is opened.
8.最后一步是打开应用程序锁对话框的按钮单击事件。
And that’s it! Biometric authentication has been implemented.
就是这样! 生物识别认证已经实施。
Here is a look at the demo:
这里是一个演示:
![Image for post](https://img-service.csdnimg.cn/img_convert/9e0a11aab8c5fa535e8273dbfab31321.png)
android实现实名认证