从回调到悬浮功能
我有此功能:
fun createBiometricPrompt(
activity: AppCompatActivity,
processSuccess: (BiometricPrompt.AuthenticationResult) -> Unit,
callback: BiometricCallback
): BiometricPrompt {
var nbFailure = 0
val executor = ContextCompat.getMainExecutor(activity)
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errCode: Int, errString: CharSequence) {
super.onAuthenticationError(errCode, errString)
Log.d(TAG, "errCode is $errCode and errString is: $errString")
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Log.d(TAG, "User biometric rejected.")
nbFailure++;
if(nbFailure == MAX_BIOMETRICS_FAILURE){
nbFailure = 0
callback.onFailure()
}
}
使用回调回调:Biometriccallback
,它仅用于使用它来通知通过发送 callback.onfailure()
回调发生的故障发生。在通过此操作的调用片段中:
fragment.kt
biometricPrompt =
BiometricPromptUtils.createBiometricPrompt(requireActivity() as AppCompatActivity, ::encryptAndStoreServerToken, object: BiometricCallback {
override fun onFailure() {
biometricPrompt.cancelAuthentication()
viewModel.loginError.set("Error Biometrics")
}
})
onfailure
被使用来取消过程并显示错误。
回调界面的定义为:
interface BiometricCallback {
fun onFailure()
}
我被要求使用悬挂函数,而不是回调,但我不知道如何正确执行此操作。我应该使用Livedata吗?如果有的话,请帮助
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
suppendCancellableCoroutine
或supstendCoroutine
转换任何基于直接样式的基于回调的API,以使其更加kotlin友好友好,而且它提供了更多的封装,您将刚刚返回结果。仅在函数中处理您
可以使用自定义对象作为结果类型来处理更多用例
you can use
suspendCancellableCoroutine
orsuspendCoroutine
to convert any callback based Api in direct style to make it more kotlin friendly plus it provides more encapsulation you will be just returned with the result all complexity is handled inside the function onlyusage
you can use custom object as result type to handle more use cases
more information