android指纹解锁
In this tutorial, we’ll be discussing the Android Fingerprint API and implement a Fingerprint Dialog in our android application.
在本教程中,我们将讨论Android Fingerprint API,并在android应用程序中实现Fingerprint Dialog。
Android指纹管理器 (Android Fingerprint Manager)
Fingerprint Manager is the class used to access the Fingerprint hardware from the device (if it exists).
指纹管理器是用于从设备(如果存在)访问指纹硬件的类。
Google recommends authenticating fingerprint in applications by displaying a DialogFragment with a Fingerprint icon to the user.
Google建议通过向用户显示带有指纹图标的DialogFragment来对应用程序中的指纹进行身份验证。
In order to implement Fingerprint Authentication, you need to add the following permission in the AndroidManifest.xml
file:
为了实现指纹认证,您需要在AndroidManifest.xml
文件中添加以下权限 :
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
Following are the steps to implement Fingerprint Authentication in your application:
以下是在您的应用程序中实现指纹认证的步骤:
- Check whether there is a secure lock on the lock screen 检查锁屏上是否有安全锁
- Check whether the Fingerprint Hardware is available using the
FingerprintManager
class. 使用FingerprintManager
类检查指纹硬件是否可用。 - Check whether the user has enrolled at least one fingerprint. 检查用户是否注册了至少一个指纹。
- Get access to Android keystore to store a key used to initiate a Cipher. 获取对Android密钥库的访问权限,以存储用于启动密码的密钥。
- Start the Authentication Method and add the callback methods 启动身份验证方法并添加回调方法
The Android Keystore system lets you store cryptographic keys in a container to make it more difficult to extract from the device.
Android Keystore系统可让您将加密密钥存储在容器中,以使其更难从设备中提取。
项目结构 (Project Structure)
码 (Code)
The code for the activity_main.xml
layout is given below:
下面给出了activity_main.xml
布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START AUTHENTICATION"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The code for the dialog_fingerprint.xml
is given below:
下面给出了dialog_fingerprint.xml
的代码:
<?xml version="1.0" e