La autenticación biométrica de huellas dactilares es un método para proteger la información confidencial o el contenido premium de su aplicación. Hoy en día, todas las aplicaciones de pago utilizan esta función en su aplicación. Es muy fácil de implementar. A continuación se muestra un video de muestra para tener una idea de lo que vamos a hacer en este artículo. Tenga en cuenta que vamos a implementar este proyecto utilizando el lenguaje Java y Kotlin .
Implementación paso a paso
Paso 1: Crear un nuevo proyecto
Para crear un nuevo proyecto en Android Studio, consulte Cómo crear/iniciar un nuevo proyecto en Android Studio . Tenga en cuenta que seleccione Kotlin/Java como lenguaje de programación
Paso 2: Agregar permiso biométrico en el manifiesto
Vaya al archivo AndroidMenifest.xml y agregue el siguiente permiso allí.
<usos-permiso android:name=”android.permission.USE_BIOMETRIC”/>
Paso 3: trabajar con el archivo activity_main.xml
Vaya al archivo activity_main.xml y consulte el siguiente código. A continuación se muestra el código para el archivo activity_main.xml . Solo tiene un botón al hacer clic en el cual se creará el cuadro de diálogo Escáner de huellas dactilares.
XML
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/start_authentication" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Authenticate" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Paso 4: trabajar con el archivo MainActivity
Vaya al archivo MainActivity y consulte el siguiente código. A continuación se muestra el código para el archivo MainActivity . Se agregan comentarios dentro del código para comprender el código con más detalle.
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<Button>(R.id.start_authentication).setOnClickListener { // This creates a dialog of biometric auth and // it requires title , subtitle , // and description // In our case there is a cancel button by // clicking it, it will cancel the process of // fingerprint authentication val biometricPrompt = BiometricPrompt.Builder(this) .setTitle("Title of Prompt") .setSubtitle("Subtitle") .setDescription("Uses FP") .setNegativeButton("Cancel", this.mainExecutor, DialogInterface.OnClickListener { dialog, which -> notifyUser("Authentication Cancelled") }).build() // start the authenticationCallback in mainExecutor biometricPrompt.authenticate(getCancellationSignal(), mainExecutor, authenticationCallback) } } // it will be called when authentication is cancelled by the user private fun getCancellationSignal(): CancellationSignal { cancellationSignal = CancellationSignal() cancellationSignal?.setOnCancelListener { notifyUser("Authentication was Cancelled by the user") } return cancellationSignal as CancellationSignal } // it checks whether the app the app has fingerprint permission @RequiresApi(Build.VERSION_CODES.M) private fun checkBiometricSupport(): Boolean { val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager if (!keyguardManager.isDeviceSecure) { notifyUser("Fingerprint authentication has not been enabled in settings") return false } if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.USE_BIOMETRIC) != PackageManager.PERMISSION_GRANTED) { notifyUser("Fingerprint Authentication Permission is not enabled") return false } return if (packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { true } else true } // this is a toast method which is responsible for showing toast // it takes a string as parameter private fun notifyUser(message: String) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show() } }
Java
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 android.os.Bundle; import android.os.CancellationSignal; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; public class MainActivity extends AppCompatActivity { // create a CancellationSignal // variable and assign a // value null to it private CancellationSignal cancellationSignal = null; // create an authenticationCallback private BiometricPrompt.AuthenticationCallback authenticationCallback; @RequiresApi(api = Build.VERSION_CODES.P) @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); authenticationCallback = new 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 public void onAuthenticationError( int errorCode, CharSequence errString) { 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 public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); notifyUser("Authentication Succeeded"); // or start a new Activity } }; checkBiometricSupport(); // create a biometric dialog on Click of button (Button) findViewById(R.id.start_authentication).setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.P) @Override public void onClick(View view) { // This creates a dialog of biometric // auth and it requires title , subtitle // , and description In our case there // is a cancel button by clicking it, it // will cancel the process of // fingerprint authentication BiometricPrompt biometricPrompt = new BiometricPrompt .Builder(getApplicationContext()) .setTitle("Title of Prompt") .setSubtitle("Subtitle") .setDescription("Uses FP") .setNegativeButton("Cancel", getMainExecutor(), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { notifyUser("Authentication Cancelled"); } }).build(); // start the authenticationCallback in // mainExecutor biometricPrompt.authenticate( getCancellationSignal(), getMainExecutor(), authenticationCallback); } }); } // it will be called when // authentication is cancelled by // the user private CancellationSignal getCancellationSignal() { cancellationSignal = new CancellationSignal(); cancellationSignal.setOnCancelListener( new CancellationSignal.OnCancelListener() { @Override public void onCancel() { notifyUser("Authentication was Cancelled by the user"); } }); return cancellationSignal; } // it checks whether the // app the app has fingerprint // permission @RequiresApi(Build.VERSION_CODES.M) private Boolean checkBiometricSupport() { KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); if (!keyguardManager.isDeviceSecure()) { notifyUser("Fingerprint authentication has not been enabled in settings"); return false; } if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.USE_BIOMETRIC)!= PackageManager.PERMISSION_GRANTED) { notifyUser("Fingerprint Authentication Permission is not enabled"); return false; } if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { return true; } else return true; } // this is a toast method which is responsible for // showing toast it takes a string as parameter private void notifyUser(String message) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } }
Producción:
Repositorio de GitHub aquí .