classUploadWorker(appContext:Context,workerParams:WorkerParameters):Worker(appContext,workerParams){overridefundoWork():Result{// Do the work here--in this case, upload the images.uploadImages()// Indicate whether the work finished successfully with the ResultreturnResult.success()}}
Java
publicclassUploadWorkerextendsWorker{publicUploadWorker(@NonNullContextcontext,@NonNullWorkerParametersparams){super(context,params);}@OverridepublicResultdoWork(){// Do the work here--in this case, upload the images.uploadImages();// Indicate whether the work finished successfully with the ResultreturnResult.success();}}
Result ที่แสดงผลจาก doWork() จะแจ้งให้บริการ WorkManager ทราบว่างานสำเร็จหรือไม่ และในกรณีที่ไม่สำเร็จ ควรลองทำงานอีกครั้งหรือไม่
[[["เข้าใจง่าย","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-08-27 UTC"],[],[],null,["To get started using WorkManager, first import the library into your Android\nproject.\n\nAdd the following dependencies to your app's `build.gradle` file: \n\nGroovy \n\n```groovy\ndependencies {\n def work_version = \"2.10.3\"\n\n // (Java only)\n implementation \"androidx.work:work-runtime:$work_version\"\n\n // Kotlin + coroutines\n implementation \"androidx.work:work-runtime-ktx:$work_version\"\n\n // optional - RxJava2 support\n implementation \"androidx.work:work-rxjava2:$work_version\"\n\n // optional - GCMNetworkManager support\n implementation \"androidx.work:work-gcm:$work_version\"\n\n // optional - Test helpers\n androidTestImplementation \"androidx.work:work-testing:$work_version\"\n\n // optional - Multiprocess support\n implementation \"androidx.work:work-multiprocess:$work_version\"\n}\n```\n\nKotlin \n\n```kotlin\ndependencies {\n val work_version = \"2.10.3\"\n\n // (Java only)\n implementation(\"androidx.work:work-runtime:$work_version\")\n\n // Kotlin + coroutines\n implementation(\"androidx.work:work-runtime-ktx:$work_version\")\n\n // optional - RxJava2 support\n implementation(\"androidx.work:work-rxjava2:$work_version\")\n\n // optional - GCMNetworkManager support\n implementation(\"androidx.work:work-gcm:$work_version\")\n\n // optional - Test helpers\n androidTestImplementation(\"androidx.work:work-testing:$work_version\")\n\n // optional - Multiprocess support\n implementation(\"androidx.work:work-multiprocess:$work_version\")\n}\n```\n\nOnce you've added the dependencies and synchronized your Gradle project, the\nnext step is to define some work to run.\n| **Note:** You can always find the latest version of WorkManager, including beta, alpha, and release candidate versions on the [WorkManager releases\n| page](/jetpack/androidx/releases/work).\n\nDefine the work\n\nWork is defined using the [Worker](/reference/androidx/work/Worker)\nclass. The `doWork()` method runs asynchronously on a background\nthread provided by WorkManager.\n\nTo create some work for WorkManager to run, extend the `Worker` class and\noverride the `doWork()` method. For example, to create a `Worker` that uploads\nimages, you can do the following: \n\nKotlin \n\n```kotlin\nclass UploadWorker(appContext: Context, workerParams: WorkerParameters):\n Worker(appContext, workerParams) {\n override fun doWork(): Result {\n\n // Do the work here--in this case, upload the images.\n uploadImages()\n\n // Indicate whether the work finished successfully with the Result\n return Result.success()\n }\n}\n```\n\nJava \n\n```java\npublic class UploadWorker extends Worker {\n public UploadWorker(\n @NonNull Context context,\n @NonNull WorkerParameters params) {\n super(context, params);\n }\n\n @Override\n public Result doWork() {\n\n // Do the work here--in this case, upload the images.\n uploadImages();\n\n // Indicate whether the work finished successfully with the Result\n return Result.success();\n }\n}\n```\n\nThe [Result](/reference/androidx/work/ListenableWorker.Result)\nreturned from `doWork()` informs the WorkManager service whether the\nwork succeeded and, in the case of failure, whether or not the work should be\nretried.\n\n- `Result.success()`: The work finished successfully.\n- `Result.failure()`: The work failed.\n- `Result.retry()`: The work failed and should be tried at another time according to its [retry policy](/topic/libraries/architecture/workmanager/how-to/define-work#retries_backoff).\n\nCreate a WorkRequest\n\nOnce your work is defined, it must be scheduled with the WorkManager service in\norder to run. WorkManager offers a lot of flexibility in how you schedule your\nwork. You can schedule it to [run\nperiodically](/topic/libraries/architecture/workmanager/how-to/define-work#schedule_periodic_work)\nover an interval of time, or you can schedule it to run only [one\ntime](/topic/libraries/architecture/workmanager/how-to/define-work#constraints).\n\nHowever you choose to schedule the work, you will always use a\n[WorkRequest](/reference/androidx/work/WorkRequest). While a\n`Worker` defines the unit of work, a\n[WorkRequest](/reference/androidx/work/WorkRequest) (and its\nsubclasses) define how and when it should be run. In the simplest case, you can\nuse a\n[OneTimeWorkRequest](/reference/androidx/work/OneTimeWorkRequest),\nas shown in the following example. \n\nKotlin \n\n```kotlin\nval uploadWorkRequest: WorkRequest =\n OneTimeWorkRequestBuilder\u003cUploadWorker\u003e()\n .build()\n```\n\nJava \n\n```java\nWorkRequest uploadWorkRequest =\n new OneTimeWorkRequest.Builder(UploadWorker.class)\n .build();\n```\n\nSubmit the WorkRequest to the system\n\nFinally, you need to submit your `WorkRequest` to `WorkManager` using the\n[enqueue()](/reference/androidx/work/WorkManager#enqueue(androidx.work.WorkRequest))\nmethod. \n\nKotlin \n\n```kotlin\nWorkManager\n .getInstance(myContext)\n .enqueue(uploadWorkRequest)\n```\n\nJava \n\n```java\nWorkManager\n .getInstance(myContext)\n .enqueue(uploadWorkRequest);\n```\n\nThe exact time that the worker is going to be executed depends on the\nconstraints that are used in your `WorkRequest` and on system optimizations.\nWorkManager is designed to give the best behavior under these restrictions.\n\nNext steps\n\nThis getting started guide only scratches the surface. The `WorkRequest` can\nalso include additional information, such as the constraints under which the\nwork should run, input to the work, a delay, and backoff policy for retrying\nwork. In the next section, [Define your work\nrequests](/topic/libraries/architecture/workmanager/how-to/define-work), you'll\nlearn more about these options in greater detail as well as get an understanding\nof how to schedule unique and reoccurring work.\n\nAdditional resources\n\nIn addition to guide documentation, there are several blogs, codelabs, and code\nsamples available to help you get started.\n\nSamples\n\n- [Sunflower](https://github.com/android/sunflower), a demo app demonstrating best practices with various architecture components, including WorkManager.\n\nCodelabs\n\n- Working with WorkManager [(Kotlin)](https://codelabs.developers.google.com/codelabs/android-workmanager/#0) and [(Java)](https://codelabs.developers.google.com/codelabs/android-workmanager-java/#0)\n- [Advanced WorkManager (Kotlin)](https://codelabs.developers.google.com/codelabs/android-adv-workmanager/#0)\n\nBlogs\n\n- [Introducing WorkManager](https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712)\n- [WorkManager Basics](https://medium.com/androiddevelopers/workmanager-basics-beba51e94048)\n- [WorkManager and Kotlin](https://medium.com/androiddevelopers/workmanager-meets-kotlin-b9ad02f7405e)\n- [WorkManager Periodicity](https://medium.com/androiddevelopers/workmanager-periodicity-ff35185ff006)\n- [Customizing WorkManager - Fundamentals](https://medium.com/androiddevelopers/customizing-workmanager-fundamentals-fdaa17c46dd2)\n- [Customize WorkManager with Dagger](https://medium.com/androiddevelopers/customizing-workmanager-with-dagger-1029688c0978)"]]