[英]Coroutine GlobalScope triggered with delay
如果處理時間超過 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
僅當處理時間超過 250 毫秒時,如何才能顯示進度對話框?
下面的代碼將顯示一個按鈕,如果工作時間超過 250 毫秒,按下該按鈕將觸發一個對話框。 在比賽中,會顯示一個小吃店,向用戶發出工作已完成的信號。 如果我理解正確,這應該展示您要求的功能。
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(應用程序):
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.