繁体   English   中英

在协程中使用 webview 回调

[英]Using webview callbacks in coroutines

I have a situation where I have a suspend function in which I want to call a webview and receive data in its shouldOverrideUrlLoading and continue the coroutine after receiving the response in the webview URL.

我还没有发现任何有用的东西。 所以我对如何实现这一点感到困惑。 另外,我将如何将线程从 Dispatchers.IO 切换到 Dispatchers.Main

所需内容的 Sudo 代码

withContext(Dispatchers.Main) {
    call api1
    call api2

    launch webview in main thread
    get result from shouldOverrideUrlLoading

    call api3 with the result from above 
    return result of api3
}

您可以使用suspendCancellableCoroutine 它旨在以一种简单的方式将回调 API 转换为挂起函数。 您可以在此处找到一些如何使用它的示例: https://kt.academy/article/cc-cancellation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM