[[["易于理解","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"]],["最后更新时间 (UTC):2025-07-30。"],[],[],null,["# App Architecture: Data Layer - Persistent Work with WorkManager - Android Developers\n\nPersistent work\nPart of [Android Jetpack](/jetpack).\n====================================================\n\nWork is persistent when it remains scheduled through app restarts and system\nreboots. [WorkManager](/reference/androidx/work/WorkManager) is the recommended solution for persistent work.\nBecause most background processing is best accomplished through persistent work,\nWorkManager is therefore also the primary recommended API for background\nprocessing in general.\n\nTypes of persistent work\n------------------------\n\nWorkManager handles three types of persistent work:\n\n- **Immediate**: Tasks that must begin immediately and complete soon. May be expedited.\n- **Long Running**: Tasks which might run for longer, potentially longer than 10 minutes.\n- **Deferrable**: Scheduled tasks that start at a later time and can run periodically.\n\nFigure 1 outlines how the different types of persistent work relate to one\nanother.\n**Figure 1**: Types of persistent work.\n\nSimilarly, the following table outlines the various types of work.\n\n| Type | Periodicity | How to access |\n|--------------|----------------------|----------------------------------------------------------------------------------------------------------|\n| Immediate | One time | `OneTimeWorkRequest` and `Worker`. For expedited work, call `setExpedited()` on your OneTimeWorkRequest. |\n| Long Running | One time or periodic | Any `WorkRequest` or `Worker`. Call `setForeground()` in the Worker to handle the notification. |\n| Deferrable | One time or periodic | `PeriodicWorkRequest` and `Worker`. |\n\nFor more information regarding how to set up WorkManager, see the [Defining your\nWorkRequests](/topic/libraries/architecture/workmanager/how-to/define-work#work-constraints) guide.\n\nWorkManager Features\n--------------------\n\nIn addition to providing a simpler and more consistent API, WorkManager has a\nnumber of other key benefits:\n\n### Work constraints\n\nDeclaratively define the optimal conditions for your work to run using [work\nconstraints](/topic/libraries/architecture/workmanager/how-to/define-work#work-constraints). For example, run only when the device is on an unmetered\nnetwork, when the device is idle, or when it has sufficient battery.\n\n### Robust scheduling\n\nWorkManager allows you to [schedule work](/topic/libraries/architecture/workmanager/how-to/define-work) to run [one-time](/reference/androidx/work/OneTimeWorkRequest) or\n[repeatedly](/reference/androidx/work/PeriodicWorkRequest) using flexible scheduling windows. Work can be tagged and named\nas well, allowing you to schedule unique, replaceable work and monitor or cancel\ngroups of work together.\n\nScheduled work is stored in an internally managed SQLite database and\nWorkManager takes care of ensuring that this work persists and is rescheduled\nacross device reboots.\n\nIn addition, WorkManager adheres to power-saving features and best practices\nlike [Doze mode](/training/monitoring-device-state/doze-standby), so you don't have to worry about it.\n\n### Expedited work\n\nYou can use WorkManager to schedule immediate work for execution in the\nbackground. You should use [Expedited work](/topic/libraries/architecture/workmanager/how-to/define-work#expedited) for tasks that are important to\nthe user and which complete within a few minutes.\n\n### Flexible retry policy\n\nSometimes work fails. WorkManager offers [flexible retry policies](/topic/libraries/architecture/workmanager/how-to/define-work#retries_backoff), including\na configurable [exponential backoff policy](/reference/androidx/work/BackoffPolicy).\n\n### Work chaining\n\nFor complex related work, [chain individual work tasks together](/topic/libraries/architecture/workmanager/how-to/chain-work) using an\nintuitive interface that allows you to control which pieces run sequentially and\nwhich run in parallel. \n\n### Kotlin\n\n```kotlin\nval continuation = WorkManager.getInstance(context)\n .beginUniqueWork(\n Constants.IMAGE_MANIPULATION_WORK_NAME,\n ExistingWorkPolicy.REPLACE,\n OneTimeWorkRequest.from(CleanupWorker::class.java)\n ).then(OneTimeWorkRequest.from(WaterColorFilterWorker::class.java))\n .then(OneTimeWorkRequest.from(GrayScaleFilterWorker::class.java))\n .then(OneTimeWorkRequest.from(BlurEffectFilterWorker::class.java))\n .then(\n if (save) {\n workRequest\u003cSaveImageToGalleryWorker\u003e(tag = Constants.TAG_OUTPUT)\n } else /* upload */ {\n workRequest\u003cUploadWorker\u003e(tag = Constants.TAG_OUTPUT)\n }\n )\n```\n\n### Java\n\n```java\nWorkManager.getInstance(...)\n.beginWith(Arrays.asList(workA, workB))\n.then(workC)\n.enqueue();\n```\n\nFor each work task, you can [define input and output data](/topic/libraries/architecture/workmanager/how-to/define-work#input_output) for that work.\nWhen chaining work together, WorkManager automatically passes output data from\none work task to the next.\n\n### Built-In threading interoperability\n\nWorkManager [integrates seamlessly](/topic/libraries/architecture/workmanager/advanced/threading) with [Coroutines](/topic/libraries/architecture/workmanager/advanced/coroutineworker) and [RxJava](/topic/libraries/architecture/workmanager/advanced/rxworker)\nand provides the flexibility to [plug in your own asynchronous APIs](/topic/libraries/architecture/workmanager/advanced/listenableworker).\n| **Note:** While Coroutines and WorkManager are recommended for different use cases, they are not mutually exclusive. You may use coroutines within work scheduled through WorkManager.\n\nUse WorkManager for reliable work\n---------------------------------\n\nWorkManager is intended for work that is required to **run reliably** even if\nthe user navigates off a screen, the app exits, or the device restarts. For\nexample:\n\n- Sending logs or analytics to backend services.\n- Periodically syncing application data with a server.\n\nWorkManager is not intended for in-process background work that can safely be\nterminated if the app process goes away. It is also not a general solution for\nall work that requires immediate execution. Please review the [background\nprocessing guide](/guide/background) to see which solution meets your needs.\n\nRelationship to other APIs\n--------------------------\n\nWhile coroutines are the recommended solution for certain use cases, you\nshouldn't use them for persistent work. It is important to note that coroutines\nis a concurrency framework, whereas WorkManager is a library for persistent\nwork. Likewise, you should use AlarmManager for clocks or calendars only.\n\n| API | Recommended for | Relationship to WorkManager |\n|------------------|-----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **Coroutines** | All asynchronous work that doesn't need to be persistent. | Coroutines are the standard means of leaving the main thread in Kotlin. However, they leave memory once the app closes. For persistent work, use WorkManager. |\n| **AlarmManager** | Alarms only. | Unlike WorkManager, AlarmManager wakes a device from Doze mode. It is therefore not efficient in terms of power and resource management. Only use it for precise alarms or notifications such as calendar events --- not background work. |\n\nReplace deprecated APIs\n-----------------------\n\nThe WorkManager API is the recommended replacement for previous Android\nbackground scheduling APIs, including [`FirebaseJobDispatcher`](/topic/libraries/architecture/workmanager/migrating-fb) and\n[`GcmNetworkManager`](/topic/libraries/architecture/workmanager/migrating-gcm).\n| **Note:** Your `FirebaseJobDispatcher` and `GcmNetworkManager` API calls no longer work on devices running Android Marshmallow (6.0) and above. Follow the migration guides for [`FirebaseJobDispatcher`](/topic/libraries/architecture/workmanager/migrating-fb) and [`GcmNetworkManager`](/topic/libraries/architecture/workmanager/migrating-gcm) for guidance on migrating.\n\nGet started\n-----------\n\nCheck out the [Getting started guide](/develop/background-work/background-tasks/persistent/getting-started) to start using WorkManager in your\napp.\n\n### Additional resources\n\nThe following sections provide some additional resources.\n\n#### Videos\n\n- [Workmanager - MAD Skills](https://www.youtube.com/playlist?list=PLWz5rJ2EKKc_J88-h0PhCO_aV0HIAs9Qk), video series\n- [Working with WorkManager](https://www.youtube.com/watch?v=83a4rYXsDs0), from the 2018 Android Dev Summit\n- [WorkManager: Beyond the basics](https://www.youtube.com/watch?v=Bz0z694SrEE), from the 2019 Android Dev Summit\n\n#### Blogs\n\n- [Introducing WorkManager](https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712)\n\n#### Samples"]]