📜  如何在不使用任何库的情况下在Android应用中添加指纹认证?

📅  最后修改于: 2021-05-09 17:32:56             🧑  作者: Mango

生物特征指纹认证是保护您的应用程序的敏感信息或高级内容的一种方法。如今,所有付款应用程序都在其应用程序中使用此功能。这很容易实现。下面提供了一个示例视频,以使您对本文将要做的事情有个大概的了解。请注意,我们将使用Kotlin语言实施该项目

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言

步骤2:在清单中添加生物识别权限

转到AndroidMenifest.xml文件,并在其中添加以下权限。

步骤3:使用activity_main.xml文件

转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。它只有一个按钮,单击该按钮将创建“指纹扫描器”对话框。

XML


  
    


Kotlin
import android.app.KeyguardManager
import android.content.Context
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.hardware.biometrics.BiometricPrompt
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CancellationSignal
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
  
class MainActivity : AppCompatActivity() {
      
    // create a CancellationSignal variable and assign a value null to it
    private var cancellationSignal: CancellationSignal? = null
  
    // create an authenticationCallback
    private val authenticationCallback: BiometricPrompt.AuthenticationCallback
        get() = @RequiresApi(Build.VERSION_CODES.P)
            object : BiometricPrompt.AuthenticationCallback() {
                // here we need to implement two methods
                // onAuthenticationError and onAuthenticationSucceeded
                // If the fingerprint is not recognized by the app it will call
                // onAuthenticationError and show a toast
                override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
                    super.onAuthenticationError(errorCode, errString)
                    notifyUser("Authentication Error : $errString")
                }
  
                // If the fingerprint is recognized by the app then it will call
                // onAuthenticationSucceeded and show a toast that Authentication has Succeed
                // Here you can also start a new activity after that
                override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
                    super.onAuthenticationSucceeded(result)
                    notifyUser("Authentication Succeeded")
                      
                    // or start a new Activity
                      
                }
            }
  
    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        checkBiometricSupport()
        // create a biometric dialog on Click of button
        findViewById


步骤4:使用MainActivity.kt文件

转到MainActivity.kt文件,并参考以下代码。下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。

科特林

import android.app.KeyguardManager
import android.content.Context
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.hardware.biometrics.BiometricPrompt
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CancellationSignal
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
  
class MainActivity : AppCompatActivity() {
      
    // create a CancellationSignal variable and assign a value null to it
    private var cancellationSignal: CancellationSignal? = null
  
    // create an authenticationCallback
    private val authenticationCallback: BiometricPrompt.AuthenticationCallback
        get() = @RequiresApi(Build.VERSION_CODES.P)
            object : BiometricPrompt.AuthenticationCallback() {
                // here we need to implement two methods
                // onAuthenticationError and onAuthenticationSucceeded
                // If the fingerprint is not recognized by the app it will call
                // onAuthenticationError and show a toast
                override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
                    super.onAuthenticationError(errorCode, errString)
                    notifyUser("Authentication Error : $errString")
                }
  
                // If the fingerprint is recognized by the app then it will call
                // onAuthenticationSucceeded and show a toast that Authentication has Succeed
                // Here you can also start a new activity after that
                override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
                    super.onAuthenticationSucceeded(result)
                    notifyUser("Authentication Succeeded")
                      
                    // or start a new Activity
                      
                }
            }
  
    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        checkBiometricSupport()
        // create a biometric dialog on Click of button
        findViewById

输出:

Github仓库在这里

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!