Skip to content

Instantly share code, notes, and snippets.

@isaidamier
Created February 19, 2020 17:59
Show Gist options
  • Save isaidamier/4ca263dd8e5b60fe585ae9d63ee3592f to your computer and use it in GitHub Desktop.
Save isaidamier/4ca263dd8e5b60fe585ae9d63ee3592f to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
...
private lateinit var biometricPrompt: BiometricPrompt
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
biometricPrompt = createBiometricPrompt()
...
}
private fun createBiometricPrompt(): BiometricPrompt {
val executor = ContextCompat.getMainExecutor(this)
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d(TAG, "$errorCode :: $errString")
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Log.d(TAG, "Authentication failed for an unknown reason")
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Log.d(TAG, "Authentication was successful")
processData(result.cryptoObject)
}
}
//The API requires the client/Activity context for displaying the prompt
val biometricPrompt = BiometricPrompt(this, executor, callback)
return biometricPrompt
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment