工作狀態
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
工作在整個生命週期中會進行一系列的 State
變更。
一次性工作狀態
如果是 one-time
工作請求,您的工作會以 ENQUEUED
狀態開始。
在 ENQUEUED
狀態下,工作只要符合 Constraints
和初始延遲時間需求,就能立即執行。此 API 會移至 RUNNING
然後根據實作結果,可能將工作移至 SUCCEEDED
、FAILED
,或回到 ENQUEUED
如果是 retry
的話。在流程中任何時間點,您都可以取消工作,這時該作業就會移至 CANCELLED
狀態。
圖 1 說明一次性工作的壽命,以及可能讓其他狀態發生的事件。

圖 1. 一次性工作的狀態圖表。
SUCCEEDED
、FAILED
和 CANCELLED
都代表這項工作的終端狀態。如果您的工作處於上述任一狀態,則 WorkInfo.State.isFinished()
會傳回 true。
週期性工作狀態
成功和失敗狀態僅適用於一次性和連鎖工作。對於週期工作而言,只有一項終端態 CANCELLED
。這是因為週期工作絕不會結束。無論結果如何,每次結果在執行後都會經過重新排程。圖 2 說明週期工作的精簡狀態圖表。

圖 2. 週期工作的狀態圖表。
封鎖狀態
我們尚未提及最終的狀態,現在是 BLOCKED
。這個狀態適用於以系列或連鎖調度管理的工作。工作鍊和其狀態圖包含在鏈鎖工作中。
後續步驟
您可以參考「管理工作」一節,進一步瞭解如何管理和監控工作進度。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Work states\n\nWork goes through a series of [`State`](/reference/androidx/work/WorkInfo.State)\nchanges over its lifetime.\n\nOne-time work states\n--------------------\n\nFor a\n[`one-time`](/topic/libraries/architecture/workmanager/how-to/define-work#schedule_one-time_work)\nwork request, your work begins in an\n[`ENQUEUED`](/reference/androidx/work/WorkInfo.State#ENQUEUED) state.\n\nIn the `ENQUEUED` state, your work is eligible to run as soon as its\n[`Constraints`](/reference/androidx/work/Constraints) and initial delay timing\nrequirements are met. From there it moves to a\n[`RUNNING`](/reference/androidx/work/WorkInfo.State#RUNNING) state and then\ndepending on the outcome of the work it may move to\n[`SUCCEEDED`](/reference/androidx/work/WorkInfo.State#SUCCEEDED),\n[`FAILED`](/reference/androidx/work/WorkInfo.State#FAILED), or possibly back to\n`ENQUEUED` if the result is\n[`retry`](/reference/androidx/work/ListenableWorker.Result#retry()). At any\npoint in the process, work can be cancelled, at which point it will move to the\n[`CANCELLED`](/reference/androidx/work/WorkInfo.State#CANCELLED) state.\n\nFigure 1 illustrates the life of one-time work, with the events that may take it to another state.\n\n**Figure 1.** State diagram for one-time work.\n\n`SUCCEEDED`, `FAILED` and `CANCELLED` all represent a terminal state for this\nwork. If your work is in any of these states,\n[`WorkInfo.State.isFinished()`](/reference/androidx/work/WorkInfo.State#isFinished())\nreturns true.\n\nPeriodic work states\n--------------------\n\nSuccess and failed states apply only to one-time and\n[chained work](/topic/libraries/architecture/workmanager/how-to/chain-work).\nFor [periodic work](/topic/libraries/architecture/workmanager/how-to/define-work#schedule_periodic_work),\nthere is only one terminal state, `CANCELLED`. This is because periodic work\nnever ends. After each run, it's rescheduled, regardless of the result. Figure\n2 depicts the condensed state diagram for periodic work.\n\n**Figure 2.** State diagram for periodic work.\n\nBlocked state\n-------------\n\nThere is one final state we haven't mentioned yet, and that is `BLOCKED`. This\nstate applies to work that is orchestrated in a series, or chain of work. Work\nchains, and their state diagram, are covered in\n[Chaining work](/topic/libraries/architecture/workmanager/how-to/chain-work).\n\nNext Steps\n==========\n\nIn [Managing work](/topic/libraries/architecture/workmanager/how-to/managing-work),\nyou'll learn more about how to manage and monitor the progress of your work."]]