Add Biometric Authentication in an Android App Using Android Studio

Introduction 

 
Biometric authentication allows users to have access to their devices and mobile apps quickly and seamlessly. Biometric authentication is used in several types of apps such as banking apps. Android provides a simple way to add a Biometric authentication feature to an app. In this article, we create a simple app that needs a Biometric Authentication to enter into the app. The same method can be used in different scenarios in the app, for example, this method can be added to your app before performing any transaction or banking operations, storing passwords using password managers, etc.
 
Step 1
 
Open Android Studio and create an empty project.
 
Step 2
 
Open MainActivity.java and add the following Objects. 
  1. Executor  executor;  
  2.    BiometricPrompt biometricPrompt;  
  3.    BiometricPrompt.PromptInfo promptInfo; 
Description
 
Excecutor: An object that executes submitted Runnable tasks. An Executor is normally used instead of explicitly creating threads.
 
BiometricPrompt: The bottom sheet which asks for Biometric Authentication.
 
Add Biometric Authentication In Android App Using Android Studio
 
BiometricPrompt.PromptInfo: PromptInfo is an inner class of BiometricPrompt which follows a builder patter to add the content inside the BiometricPrompt.
 
Step 3
 
Initialize the objects as shown below: 
  1. executor = ContextCompat.getMainExecutor(this); 
Description
 
ContextCompat is similar to getContext() used prior to AndroidX:
  1. biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {  
  2.           @Override  
  3.           public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {  
  4.               super.onAuthenticationSucceeded(result);  
  5.   
  6.               Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show();  
  7.           }  
  8.   
  9.           @Override  
  10.           public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {  
  11.               super.onAuthenticationError(errorCode, errString);  
  12.   
  13.               Toast.makeText(MainActivity.this,errString,Toast.LENGTH_LONG).show();  
  14.               MainActivity.this.finish();  
  15.           }  
  16.   
  17.           @Override  
  18.           public void onAuthenticationFailed() {  
  19.               super.onAuthenticationFailed();  
  20.   
  21.               Toast.makeText(MainActivity.this,"FAILED",Toast.LENGTH_LONG).show();  
  22.           }  
  23.       }); 
Description
 
The BiometricPrompt constructor takes three arguments context of the activity, i.e this excecutor object and a CallBack methods to handle what should happen when the Authentication succeeds, Authentication fails and the Authentication error that occurs when the user closes the BiometricPrompt, in our case when the user closes the BiometricPrompt, the app exits by calling finish() activity lifecycle method. 
  1. promptInfo = new BiometricPrompt.PromptInfo.Builder()  
  2.                .setTitle("Touch id required")  
  3.                .setDescription("Touch the touch id sensor")  
  4.                .setNegativeButtonText("Exit")  
  5.                .build();  
  6.   
  7.        biometricPrompt.authenticate(promptInfo); 
Here we initialize the promptInfo object by using a Builder pattern in Java.
 
The entire MainActivity.java file code:
  1. package com.enigmabits.myapplication;  
  2.   
  3. import androidx.annotation.NonNull;  
  4. import androidx.appcompat.app.AppCompatActivity;  
  5. import androidx.biometric.BiometricPrompt;  
  6. import androidx.core.content.ContextCompat;  
  7.   
  8. import android.os.Bundle;  
  9. import android.widget.Toast;  
  10.   
  11. import java.util.concurrent.Executor;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.     Executor  executor;  
  15.     BiometricPrompt biometricPrompt;  
  16.     BiometricPrompt.PromptInfo promptInfo;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         executor = ContextCompat.getMainExecutor(this);  
  23.   
  24.         biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {  
  25.             @Override  
  26.             public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {  
  27.                 super.onAuthenticationSucceeded(result);  
  28.   
  29.                 Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show();  
  30.             }  
  31.   
  32.             @Override  
  33.             public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {  
  34.                 super.onAuthenticationError(errorCode, errString);  
  35.   
  36.                 Toast.makeText(MainActivity.this,errString,Toast.LENGTH_LONG).show();  
  37.                 MainActivity.this.finish();  
  38.             }  
  39.   
  40.             @Override  
  41.             public void onAuthenticationFailed() {  
  42.                 super.onAuthenticationFailed();  
  43.   
  44.                 Toast.makeText(MainActivity.this,"FAILED",Toast.LENGTH_LONG).show();  
  45.             }  
  46.         });  
  47.   
  48.   
  49.         promptInfo = new BiometricPrompt.PromptInfo.Builder()  
  50.                 .setTitle("Touch id required")  
  51.                 .setDescription("Touch the touch id sensor")  
  52.                 .setNegativeButtonText("Exit")  
  53.                 .build();  
  54.   
  55.         biometricPrompt.authenticate(promptInfo);  
  56.   
  57.     }  

Now run your app in your physical device or emulator. Make sure you've already registered a fingerprint in the Android settings.
 
For a video tutorial, please refer here.


Similar Articles