全局作用域协程延时触发

3

如果处理时间超过250毫秒,我希望显示进度对话框。

我正在尝试使用协程来完成这个任务。我的问题是,无论我是否删除“delay(250)”行,它总是首先运行dismissProgressDialog(),然后才运行showProgressDialog()。我认为这与GlobalScope.launch(Dispatchers.Main)有关,因为当我删除它时,它会按预期先运行showProgressDialog()。

fun showProgressDialog() =
    GlobalScope.launch(Dispatchers.Main) {
        if (!customDialog.isShowing) {
            forceCloseLoading = false

            LogUtils.e("progress show before delay")
            delay(250)
            LogUtils.e("progress show after delay")

            if (!forceCloseLoading) {
                customDialog.show()
            }
        }
    }

fun dismissProgressDialog() {
    forceCloseLoading = true
    LogUtils.e("progress dismiss")

    try {
        if (customDialog.isShowing) {
            customDialog.dismiss()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

}

以下是我的logcat输出:

progress dismiss
progress show before delay
progress show after delay

如果处理时间大于250ms,我该如何仅显示进度对话框?


请问您能否分享调用 showProgressDialogdismissProgressDialog 的代码?看起来有些东西正在阻塞您的主线程,从而防止 showProgressDialog 被安排运行。您可以使用 launch(Dispatchers.Main, start=CoroutineState.UNDISPATCHED) 强制它运行直到在 delay() 处暂停。 - Roman Elizarov
1个回答

1
下面的代码将显示一个按钮,当按下时,如果工作时间超过250毫秒,将触发对话框。完成后,会显示一个 snackbar,向用户信号表明工作已完成。如果我理解正确,这应该展示了你所要求的功能。
class MainActivity : AppCompatActivity() {
    // Create a scope for out coroutines to run on, with the coroutine context Unconfined.
    private val scope: CoroutineScope = CoroutineScope(Dispatchers.Unconfined)

    private lateinit var customDialogBuilder: AlertDialog.Builder
    private lateinit var customDialog: AlertDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Create a dialog to display when task is in progress
        customDialogBuilder = AlertDialog.Builder(this)
            .setMessage("Working on it...")
            .setTitle("Loading...")

        // Set the listener where a coroutine job is launched. The job starts another job which will
        // take somewhere between 1..800 ms to complete. The parent job waits 250ms then checks if
        // the child job is completed, if not it displays the dialog.
        btn_process.setOnClickListener {
            scope.launch {

                // Launch a job with the work we are waiting on.
                val job = launchWork()
                delay(250)

                // Check if the job is still active, if so show the dialog.
                if (job.isActive) {
                    runOnUiThread {
                        customDialog = customDialogBuilder.show()
                    }
                }
            }
        }
    }

    private fun launchWork(): Job {
        return scope.launch {
            // Simulate some work.
            val workTime = Random.nextLong(1, 800)
            delay(workTime)

            runOnUiThread {
                // Dismiss dialog when done.
                if (customDialog.isShowing) {
                    customDialog.dismiss()
                }
                // Show some feedback that the work is done.
                Snackbar
                    .make(layout_root, "Work finished in ${workTime}ms", Snackbar.LENGTH_LONG)
                    .show()
            }
        }
    }
}

布局,activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/layout_root"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_process"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Process" />

</LinearLayout>

build.gradle (app):

dependencies {

    ...

    def coroutines_version = "1.3.1"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

    implementation 'com.google.android.material:material:1.0.0' // For snackbar

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接