Biometric factors allow for secure authentication on the Android platform.
BattlefieldFinger Print Unlock | Face Unlock |
---|---|
def biometric_version= '1.2.0-alpha04'
implementation "androidx.biometric:biometric:$biometric_version"
val biometricManager = BiometricManager.from(context)
if(biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS){
// you can authenticate with biometrics
}
private fun instanceOfBiometricPrompt(): BiometricPrompt {
val executor = ContextCompat.getmainExecutor(context)
val callback = object:BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
showMessage("$errorCode :: $errString")
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
showMessage("Authentication failed for an unknown reason")
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult{
super.onAuthenticationSucceeded(result)
showMessage("Authentication was successful")
}
}
val biometricPrompt = BiometricPrompt(context, executor, callback)
return biometricPrompt
}
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
// Can't call setNegativeButtonText() and // setAllowedAuthenticators(... or DEVICE_CREDENTIAL) at the same time.//
//.setNegativeButtonText("Use account password") //
.setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
.build()
biometricPrompt.authenticate(promptInfo)
keyguardManager = getSystemService(KEYGUARD_SERVICE) as KeyguardManager
if (keyguardManager.isKeyguardSecure) {
//Screen lock is enabled, do authentication.
}
startActivityForResult(Intent(Settings.ACTION_SECURITY_SETTINGS), REQUEST_CODE)
For more info go to Android Developers Biometric Blog
Support it by joining stargazers for this repository. ⭐
Copyright 2022 Simform Solutions
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.