Android 延迟触发的协程全局镜
如果进程时间超过250ms,我想显示进度对话框 我正试着和同事一起做。我的问题是,无论我是否删除“delay(250)”行,它总是先运行dismissProgressDialog(),然后再运行showProgressDialog()。我认为这是关于GlobalScope.launch(Dispatchers.Main)的,因为当我删除它时,它会按照预期首先运行showProgressDialog()Android 延迟触发的协程全局镜,android,kotlin,coroutine,kotlin-coroutines,Android,Kotlin,Coroutine,Kotlin Coroutines,如果进程时间超过250ms,我想显示进度对话框 我正试着和同事一起做。我的问题是,无论我是否删除“delay(250)”行,它总是先运行dismissProgressDialog(),然后再运行showProgressDialog()。我认为这是关于GlobalScope.launch(Dispatchers.Main)的,因为当我删除它时,它会按照预期首先运行showProgressDialog() fun showProgressDialog() = GlobalScope.laun
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时,如何显示“进度”对话框?下面的代码将显示一个按钮,如果工作时间超过250ms,按下该按钮将触发对话框。比赛结束后,屏幕上会显示一个小吃条,向用户发出工作完成的信号。如果我理解正确,这将演示您要求的功能
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>
您还可以共享调用
showProgressDialog
和dismissProgressDialog
的代码吗?看起来有什么东西阻塞了主线程,阻止了showProgressDialog
计划运行。您可以使用launch(Dispatchers.Main,start=CoroutineState.UNDISPATCHED)
强制它立即在delay()暂停运行。
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
}