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.
- Executor executor;
- BiometricPrompt biometricPrompt;
- 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.
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:
- executor = ContextCompat.getMainExecutor(this);
Description
ContextCompat is similar to getContext() used prior to AndroidX:
- biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
- @Override
- public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
- super.onAuthenticationSucceeded(result);
-
- Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show();
- }
-
- @Override
- public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
- super.onAuthenticationError(errorCode, errString);
-
- Toast.makeText(MainActivity.this,errString,Toast.LENGTH_LONG).show();
- MainActivity.this.finish();
- }
-
- @Override
- public void onAuthenticationFailed() {
- super.onAuthenticationFailed();
-
- Toast.makeText(MainActivity.this,"FAILED",Toast.LENGTH_LONG).show();
- }
- });
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.
- promptInfo = new BiometricPrompt.PromptInfo.Builder()
- .setTitle("Touch id required")
- .setDescription("Touch the touch id sensor")
- .setNegativeButtonText("Exit")
- .build();
-
- biometricPrompt.authenticate(promptInfo);
Here we initialize the promptInfo object by using a Builder pattern in Java.
The entire MainActivity.java file code:
- package com.enigmabits.myapplication;
-
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.biometric.BiometricPrompt;
- import androidx.core.content.ContextCompat;
-
- import android.os.Bundle;
- import android.widget.Toast;
-
- import java.util.concurrent.Executor;
-
- public class MainActivity extends AppCompatActivity {
- Executor executor;
- BiometricPrompt biometricPrompt;
- BiometricPrompt.PromptInfo promptInfo;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- executor = ContextCompat.getMainExecutor(this);
-
- biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
- @Override
- public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
- super.onAuthenticationSucceeded(result);
-
- Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show();
- }
-
- @Override
- public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
- super.onAuthenticationError(errorCode, errString);
-
- Toast.makeText(MainActivity.this,errString,Toast.LENGTH_LONG).show();
- MainActivity.this.finish();
- }
-
- @Override
- public void onAuthenticationFailed() {
- super.onAuthenticationFailed();
-
- Toast.makeText(MainActivity.this,"FAILED",Toast.LENGTH_LONG).show();
- }
- });
-
-
- promptInfo = new BiometricPrompt.PromptInfo.Builder()
- .setTitle("Touch id required")
- .setDescription("Touch the touch id sensor")
- .setNegativeButtonText("Exit")
- .build();
-
- biometricPrompt.authenticate(promptInfo);
-
- }
- }
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.