Blog Infos
Author
Published
Topics
,
Author
Published

Android 12 provides new APIs to improve our applications’ biometric authentication user experience.

BiometricManager.Strings class added to the BiometricManager which has 3 methods that expose the String values that we can use for prompt message, button label, and settings label.

Methods
  1. getButtonLabel(): returns a localized string that can be used as the text of a view that invokes BiometricPrompt(e.g button’s text)

 

Button’s text

 

  • getPromptMessage(): returns a localized string that can be shown while the user is authenticating with BiometricPrompt.
  • getSettingName(): returns a localized string that can be shown as the textfor an app setting that enables authentication with BiometricPrompt(e.g switch’s text).

 

 

All the methods may return null if none of the requested authenticator types are available.

Returned strings depend on which authentication methods are available on the device:

Results from Samsung S21 Ultra
When user has only face id:
[Use Face Unlock,
Use your face to continue,
Use biometrics]
When user has only fingerprint:
[Use fingerprint,
Use your fingerprint to continue,
Use biometrics]
When user has both face id and fingerprint:
[Use biometrics,
Use your biometric to continue,
Use biometrics]

Messages based on the authentication modes

 

Implementation 🧑‍💻
  • Add biometric permission in AndroidManifest.xml
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>

AndroidManifest.xml

 

  • Add Androidx’s biometric library dependency inbuild.gradle(app)
implementation "androidx.biometric:biometric:1.1.0"

build.gradle (app level)

 

  • Finally, add the following code in Activity/Fragment
val biometricManager = BiometricManager.from(this)
var result = listOf<String?>()
if (biometricManager.canAuthenticate(BIOMETRIC_STRONG) == BiometricManager.BIOMETRIC_SUCCESS) {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val bioMetricHardwareManager = (applicationContext.getSystemService(Context.BIOMETRIC_SERVICE) as? android.hardware.biometrics.BiometricManager)
bioMetricHardwareManager?.getStrings(Authenticators.BIOMETRIC_WEAK or Authenticators.BIOMETRIC_STRONG)
?.run {
result = listOf(
buttonLabel?.toString(),
promptMessage?.toString(),
settingName?.toString()
)
}
}
else -> {
// NOT_AVAILABLE_FOR <API 12
}
}
}

Job Offers

Job Offers


    Senior Android Developer

    SumUp
    Berlin
    • Full Time
    apply now

    Senior Android Engineer

    Carly Solutions GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

No results found.

Jobs

  • We need to have an instance of BiometricManager to check if biometrics is available or not (line3 in the above gist file)
  • After getting the true result from canAuthenticate() method we can move forward and create an instance of android.hardware.biometrics.BiometricManager
  • Access the getStrings() method and access all the methods to get the localized string values.
😊😊 👏👏👏👏 HAPPY CODING 👏👏👏👏 😊😊
https://www.buymeacoffee.com/navczydev
Stay in touch

 

This article was originally published on proandroiddev.com on April 15, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
In this article, we will go through the improvements that Android12 brings for Exact…
READ MORE
blog
Splash screen is the initial screen you’ll see when you open your app. It…
READ MORE
blog
Android 12 brought several changes and one of them, that raises some questions, is…
READ MORE
blog
During I/O 2021, Google announced the latest stage for Material Design, Material You (or…
READ MORE

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu