สังเกตความคืบหน้าของผู้ปฏิบัติงานระดับกลาง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
WorkManager มีการรองรับในตัวสำหรับการตั้งค่าและสังเกตความคืบหน้าระหว่างกลางสำหรับ Worker
หาก Worker ทำงานขณะที่แอปอยู่ในเบื้องหน้า ระบบจะแสดงข้อมูลนี้ต่อผู้ใช้ได้ด้วย API ที่แสดง LiveData
ของ WorkInfo
ListenableWorker
รองรับ API
setProgressAsync()
แล้ว ซึ่งช่วยให้เก็บความคืบหน้าชั่วคราวได้ API เหล่านี้ช่วยให้
นักพัฒนาแอปตั้งค่าความคืบหน้าระดับกลางที่ UI สังเกตได้
ความคืบหน้าแสดงด้วยประเภท Data
ซึ่งเป็นคอนเทนเนอร์ของพร็อพเพอร์ตี้ที่สามารถแปลงเป็นอนุกรมได้ (คล้ายกับ input
และ
output
และอยู่ภายใต้ข้อจำกัดเดียวกัน)
คุณจะดูและอัปเดตข้อมูลความคืบหน้าได้ขณะที่
ListenableWorker
ทำงานอยู่เท่านั้น ระบบจะไม่สนใจความพยายามในการตั้งค่าความคืบหน้าใน ListenableWorker
หลังจากที่ดำเนินการเสร็จสมบูรณ์แล้ว
นอกจากนี้ คุณยังดูข้อมูลความคืบหน้าได้โดยใช้วิธีใดวิธีหนึ่งต่อไปนี้ getWorkInfoBy…()
หรือ
getWorkInfoBy…LiveData()
เมธอดเหล่านี้จะแสดงอินสแตนซ์ของ
WorkInfo
ซึ่งมีเมธอด getProgress()
ใหม่
ที่แสดง Data
ความคืบหน้าในการอัปเดต
สำหรับนักพัฒนาซอฟต์แวร์ Java ที่ใช้ ListenableWorker
หรือ Worker
API setProgressAsync()
จะแสดงผล ListenableFuture<Void>
การอัปเดตความคืบหน้าเป็นแบบอะซิงโครนัส
เนื่องจากกระบวนการอัปเดตเกี่ยวข้องกับการจัดเก็บข้อมูลความคืบหน้าในฐานข้อมูล
ใน Kotlin คุณสามารถใช้ฟังก์ชันส่วนขยาย CoroutineWorker
ของออบเจ็กต์ setProgress()
เพื่ออัปเดตข้อมูลความคืบหน้า
ตัวอย่างนี้แสดง ProgressWorker
Worker
จะตั้งค่าความคืบหน้าเป็น 0 เมื่อเริ่มทำงาน และเมื่อเสร็จสมบูรณ์แล้วจะอัปเดตค่าความคืบหน้าเป็น 100
Kotlin
import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.Data
import androidx.work.WorkerParameters
import kotlinx.coroutines.delay
class ProgressWorker(context: Context, parameters: WorkerParameters) :
CoroutineWorker(context, parameters) {
companion object {
const val Progress = "Progress"
private const val delayDuration = 1L
}
override suspend fun doWork(): Result {
val firstUpdate = workDataOf(Progress to 0)
val lastUpdate = workDataOf(Progress to 100)
setProgress(firstUpdate)
delay(delayDuration)
setProgress(lastUpdate)
return Result.success()
}
}
Java
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
public class ProgressWorker extends Worker {
private static final String PROGRESS = "PROGRESS";
private static final long DELAY = 1000L;
public ProgressWorker(
@NonNull Context context,
@NonNull WorkerParameters parameters) {
super(context, parameters);
// Set initial progress to 0
setProgressAsync(new Data.Builder().putInt(PROGRESS, 0).build());
}
@NonNull
@Override
public Result doWork() {
try {
// Doing work.
Thread.sleep(DELAY);
} catch (InterruptedException exception) {
// ... handle exception
}
// Set progress to 100 after you are done doing your work.
setProgressAsync(new Data.Builder().putInt(PROGRESS, 100).build());
return Result.success();
}
}
การสังเกตความคืบหน้า
หากต้องการดูข้อมูลความคืบหน้า ให้ใช้วิธีการ getWorkInfoById
และรับข้อมูลอ้างอิงถึง
WorkInfo
ต่อไปนี้คือตัวอย่างที่ใช้ getWorkInfoByIdFlow
สำหรับ Kotlin และ
getWorkInfoByIdLiveData
สำหรับ Java
Kotlin
WorkManager.getInstance(applicationContext)
// requestId is the WorkRequest id
.getWorkInfoByIdFlow(requestId)
.collect { workInfo: WorkInfo? ->
if (workInfo != null) {
val progress = workInfo.progress
val value = progress.getInt("Progress", 0)
// Do something with progress information
}
}
Java
WorkManager.getInstance(getApplicationContext())
// requestId is the WorkRequest id
.getWorkInfoByIdLiveData(requestId)
.observe(lifecycleOwner, new Observer<WorkInfo>() {
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null) {
Data progress = workInfo.getProgress();
int value = progress.getInt(PROGRESS, 0)
// Do something with progress
}
}
});
อ่านข้อมูลเพิ่มเติมเกี่ยวกับเอกสารประกอบในการสังเกตWorker
ออบเจ็กต์ได้ที่หัวข้อ
สถานะงานและการสังเกตงาน
ดูวิธีรับ stopReason
เมื่อมีการสิ้นสุดงานโดยไม่คาดคิดได้ที่สังเกตสถานะเหตุผลการหยุด
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-22 UTC
[null,null,["อัปเดตล่าสุด 2025-08-22 UTC"],[],[],null,["# Observe intermediate worker progress\n\nWorkManager has built-in support for setting and observing intermediate\nprogress for workers. If the worker was running while the app was in the\nforeground, this information can also be shown to the user using APIs which\nreturn the [`LiveData`](/reference/androidx/lifecycle/LiveData) of\n[`WorkInfo`](/reference/androidx/work/WorkInfo).\n\n[`ListenableWorker`](/reference/androidx/work/ListenableWorker) now supports the\n[`setProgressAsync()`](/reference/androidx/work/ListenableWorker#setProgressAsync(androidx.work.Data))\nAPI, which allows it to persist intermediate progress. These APIs allow\ndevelopers to set intermediate progress that can be observed by the UI.\nProgress is represented by the [`Data`](/reference/androidx/work/Data) type,\nwhich is a serializable container of properties (similar to [`input` and\n`output`](/topic/libraries/architecture/workmanager/advanced#params),\nand subject to the same restrictions).\n\nProgress information can only be observed and updated while the\n`ListenableWorker` is running. Attempts to set progress on a `ListenableWorker`\nafter it has completed its execution are ignored.\n\nYou can also observe progress\ninformation by using the one of the [`getWorkInfoBy...()` or\n`getWorkInfoBy...LiveData()`](/reference/androidx/work/WorkManager#getWorkInfoById(java.util.UUID))\nmethods. These methods return instances of\n[`WorkInfo`](/reference/androidx/work/WorkInfo), which has a new\n[`getProgress()`](/reference/androidx/work/WorkInfo#getProgress()) method\nthat returns `Data`.\n\nUpdate Progress\n---------------\n\nFor Java developers using a [`ListenableWorker`](/reference/androidx/work/ListenableWorker)\nor a [`Worker`](/reference/androidx/work/Worker), the\n[`setProgressAsync()`](/reference/androidx/work/ListenableWorker#setProgressAsync(androidx.work.Data))\nAPI returns a `ListenableFuture\u003cVoid\u003e`; updating progress is asynchronous,\ngiven that the update process involves storing progress information in a database.\nIn Kotlin, you can use the [`CoroutineWorker`](/reference/kotlin/androidx/work/CoroutineWorker)\nobject's [`setProgress()`](/reference/kotlin/androidx/work/CoroutineWorker#setprogress)\nextension function to update progress information.\n\nThis example shows a `ProgressWorker`. The `Worker` sets its progress to\n0 when it starts, and upon completion updates the progress value to 100. \n\n### Kotlin\n\n import android.content.Context\n import androidx.work.CoroutineWorker\n import androidx.work.Data\n import androidx.work.WorkerParameters\n import kotlinx.coroutines.delay\n\n class ProgressWorker(context: Context, parameters: WorkerParameters) :\n CoroutineWorker(context, parameters) {\n\n companion object {\n const val Progress = \"Progress\"\n private const val delayDuration = 1L\n }\n\n override suspend fun doWork(): Result {\n val firstUpdate = workDataOf(Progress to 0)\n val lastUpdate = workDataOf(Progress to 100)\n setProgress(firstUpdate)\n delay(delayDuration)\n setProgress(lastUpdate)\n return Result.success()\n }\n }\n\n### Java\n\n import android.content.Context;\n import androidx.annotation.NonNull;\n import androidx.work.Data;\n import androidx.work.Worker;\n import androidx.work.WorkerParameters;\n\n public class ProgressWorker extends Worker {\n\n private static final String PROGRESS = \"PROGRESS\";\n private static final long DELAY = 1000L;\n\n public ProgressWorker(\n @NonNull Context context,\n @NonNull WorkerParameters parameters) {\n super(context, parameters);\n // Set initial progress to 0\n setProgressAsync(new Data.Builder().putInt(PROGRESS, 0).build());\n }\n\n @NonNull\n @Override\n public Result doWork() {\n try {\n // Doing work.\n Thread.sleep(DELAY);\n } catch (InterruptedException exception) {\n // ... handle exception\n }\n // Set progress to 100 after you are done doing your work.\n setProgressAsync(new Data.Builder().putInt(PROGRESS, 100).build());\n return Result.success();\n }\n }\n\nObserving Progress\n------------------\n\nTo observe progress information, use the [`getWorkInfoById`](/reference/androidx/work/WorkManager#getWorkInfoById(java.util.UUID)) methods, and get a reference to\n[`WorkInfo`](/reference/androidx/work/WorkInfo).\n\nHere is an example which uses `getWorkInfoByIdFlow` for Kotlin and\n`getWorkInfoByIdLiveData` for Java. \n\n### Kotlin\n\n WorkManager.getInstance(applicationContext)\n // requestId is the WorkRequest id\n .getWorkInfoByIdFlow(requestId)\n .collect { workInfo: WorkInfo? -\u003e\n if (workInfo != null) {\n val progress = workInfo.progress\n val value = progress.getInt(\"Progress\", 0)\n // Do something with progress information\n }\n }\n\n### Java\n\n WorkManager.getInstance(getApplicationContext())\n // requestId is the WorkRequest id\n .getWorkInfoByIdLiveData(requestId)\n .observe(lifecycleOwner, new Observer\u003cWorkInfo\u003e() {\n @Override\n public void onChanged(@Nullable WorkInfo workInfo) {\n if (workInfo != null) {\n Data progress = workInfo.getProgress();\n int value = progress.getInt(PROGRESS, 0)\n // Do something with progress\n }\n }\n });\n\nFor more documentation on observing `Worker` objects, read\n[Work States and observing work](/topic/libraries/architecture/workmanager/how-to/states-and-observation).\nTo learn how to get the [stopReason](/reference/androidx/work/WorkInfo#getStopReason())\nwhen work terminates unexpectedly, reference [Observe stop reason state](/develop/background-work/background-tasks/persistent/how-to/manage-work#stop-reason)."]]