[[["易于理解","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,["When you want to execute tasks that will continue to run even if the app leaves\nthe visible state, we recommend using the Jetpack library [WorkManager](/reference/androidx/work/WorkManager).\nWorkManager features a robust scheduling mechanism that lets tasks persist\nacross app restarts and device reboots.\n\nTypes of work\n\nWorkManager handles three types of 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 tasks relate to one\nanother.\n**Figure 1**: Types of 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\nIn addition to providing a simpler and more consistent API, WorkManager has a\nnumber of other key benefits:\n\nWork 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\nRobust 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\nExpedited 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\nFlexible 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\nWork 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\nKotlin \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\nJava \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\nBuilt-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\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\nThis table shows how WorkManager relates to similar APIs. This information can\nhelp you choose the right API for your app's requirements.\n\n| API | Recommended for | Relationship to WorkManager |\n|------------------|-----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **Coroutines** | All asynchronous work that doesn't need to persist if the app leaves the visible state. | Coroutines are the standard means of leaving the main thread in Kotlin. However, they stop as soon as the app closes. For work that should persist even after the app closes, use WorkManager. |\n| **AlarmManager** | Alarms only. | Unlike WorkManager's regular workers, AlarmManager's exact alarms wake 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 for recurring background work. |\n\nReplace deprecated APIs\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\nCheck out the [Getting started guide](/develop/background-work/background-tasks/persistent/getting-started) to start using WorkManager in your\napp.\n\nAdditional resources\n\nThe following sections provide some additional resources.\n\nVideos\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\nBlogs\n\n- [Introducing WorkManager](https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712)\n\nSamples"]]