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
- getButtonLabel(): returns a localized string that can be used as the
text
of a view that invokesBiometricPrompt
(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
text
for an app setting that enables authentication withBiometricPrompt
(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 in
build.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
- 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 fromcanAuthenticate()
method we can move forward and create an instance ofandroid.hardware.biometrics.BiometricManager
- Access the
getStrings()
method and access all the methods to get thelocalized string
values.
Check the full code here
References
https://developer.android.com/reference/android/hardware/biometrics/BiometricManager.Strings
https://developer.android.com/reference/android/hardware/biometrics/BiometricManager
😊😊 👏👏👏👏 HAPPY CODING 👏👏👏👏 😊😊
This article was originally published on proandroiddev.com on April 15, 2022