How to Integrate Android Fingerprint API to Confirm User’s Identity With Single Touch

Recently, with the release of Android Marshmallow, there has been significant changes to the Android APIs, including Android Fingerprint API. This new API brings user authentication to a whole new level by making it fast and secure.

This new feature allows developers to confirm the users’ identify with a single touch. And, while this new security feature is commonly used to secure lockscreen, but it’s also possible to add this feature into Android apps by integrating Android Fingerprint API.

In this Android app tutorial, we’ll build Android app for example to see exactly how to integrate Android Fingerprint API.

Let’s Get Started

Open your Android Studio and create a new project.


Select targeted Android device and click on next.


In the next tab, add Empty Activity.


Lastly, enter your activity name.


Start Code Integration


<manifest xmlns:android=""
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<activity android:name=".FingerprintActivity"
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

public class FingerprintActivity extends AppCompatActivity {

private KeyStore keyStore;
// Variable used for storing the key in the Android Keystore container
private static final String KEY_NAME = "androidHive";
private Cipher cipher;
private TextView textView;

protected void onCreate(Bundle savedInstanceState) {

// Initializing both Android Keyguard Manager and Fingerprint Manager
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);

textView = (TextView) findViewById(;

// Check whether the device has a Fingerprint sensor.
* An error message will be displayed if the device does not contain the fingerprint hardware.
* However if you plan to implement a default authentication method,
* you can redirect the user to a default authentication activity from here.
* Example:
* Intent intent = new Intent(this, DefaultAuthenticationActivity.class);
* startActivity(intent);
textView.setText("Your Device does not have a Fingerprint Sensor");
}else {
// Checks whether fingerprint permission is set on manifest
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
textView.setText("Fingerprint authentication permission not enabled");
// Check whether at least one fingerprint is registered
if (!fingerprintManager.hasEnrolledFingerprints()) {
textView.setText("Register at least one fingerprint in Settings");
// Checks whether lock screen security is enabled or not
if (!keyguardManager.isKeyguardSecure()) {
textView.setText("Lock screen security not enabled in Settings");

if (cipherInit()) {
FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
FingerprintHandler helper = new FingerprintHandler(this);
helper.startAuth(fingerprintManager, cryptoObject);

protected void generateKey() {
try {
keyStore = KeyStore.getInstance("AndroidKeyStore");
} catch (Exception e) {

KeyGenerator keyGenerator;
try {
keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new RuntimeException("Failed to get KeyGenerator instance", e);

try {
} catch (NoSuchAlgorithmException |
| CertificateException | IOException e) {
throw new RuntimeException(e);

public boolean cipherInit() {
try {
cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException("Failed to get Cipher", e);

try {
SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
cipher.init(Cipher.ENCRYPT_MODE, key);
return true;
} catch (KeyPermanentlyInvalidatedException e) {
return false;
} catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException("Failed to init Cipher", e);

public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {

private Context context;

// Constructor
public FingerprintHandler(Context mContext) {
context = mContext;

public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
CancellationSignal cancellationSignal = new CancellationSignal();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);

public void onAuthenticationError(int errMsgId, CharSequence errString) {
this.update("Fingerprint Authentication error\n" + errString, false);

public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
this.update("Fingerprint Authentication help\n" + helpString, false);

public void onAuthenticationFailed() {
this.update("Fingerprint Authentication failed.", false);

public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
this.update("Fingerprint Authentication succeeded.", true);

public void update(String e, Boolean success){
TextView textView = (TextView) ((Activity)context).findViewById(;

That’s it!


Now, there are many ways you can use this kind of single-touch identification to improve user experience in your Android app. But, if you’re planning to integrate the Android fingerprint API for a particular service like making payments or accessing a specific feature, then consult with Android app development company to add this extra layer of security to your app features.

Remember, fingerprint authentication can help to remove the need for users to enter password every time they use app to increase user experience of your mobile app.

Grab a free copy of Fingerprint Authentication Demo from Github.

This page was last edited on July 5th, 2018, at 4:13.

Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now

Leave a Reply

Your email address will not be published. Required fields are marked *