Gửi yêu cầu công việc đến dịch vụ dưới nền
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bài học trước đã hướng dẫn bạn cách tạo
Lớp JobIntentService
. Chiến dịch này
bài học này sẽ hướng dẫn bạn cách kích hoạt
JobIntentService
để chạy một thao tác theo
thêm công việc vào hàng đợi bằng Intent
.
Intent
này có thể
(không bắt buộc) có chứa dữ liệu cho
JobIntentService
để xử lý.
Tạo và gửi yêu cầu công việc đến JobIntentService
Để tạo một yêu cầu công việc và gửi nó đến
JobIntentService
!
tạo một Intent
rồi thêm đối tượng này vào hàng đợi
được thực thi bằng cách gọi
enqueueWork()
.
Nếu muốn, bạn có thể thêm dữ liệu vào ý định (ở dạng bổ sung ý định) cho
JobIntentService để xử lý. Để biết thêm thông tin về cách tạo ý định, hãy đọc tài liệu Xây dựng
phần ý định trong Ý định và bộ lọc ý định
Các đoạn mã sau đây minh hoạ quá trình này:
-
Tạo một
Intent
mới cho
JobIntentService
đã gọi RSSPullService
.
Kotlin
/*
* Creates a new Intent to start the RSSPullService
* JobIntentService. Passes a URI in the
* Intent's "data" field.
*/
serviceIntent = Intent().apply {
putExtra("download_url", dataUrl)
}
Java
/*
* Creates a new Intent to start the RSSPullService
* JobIntentService. Passes a URI in the
* Intent's "data" field.
*/
serviceIntent = new Intent();
serviceIntent.putExtra("download_url", dataUrl));
-
Gọi
enqueueWork()
Kotlin
private const val RSS_JOB_ID = 1000
RSSPullService.enqueueWork(context, RSSPullService::class.java, RSS_JOB_ID, serviceIntent)
Java
// Starts the JobIntentService
private static final int RSS_JOB_ID = 1000;
RSSPullService.enqueueWork(getContext(), RSSPullService.class, RSS_JOB_ID, serviceIntent);
Xin lưu ý rằng bạn có thể gửi yêu cầu công việc từ bất cứ đâu trong một Hoạt động hoặc Mảnh.
Ví dụ: trước tiên, nếu cần nhận hoạt động đầu vào của người dùng, bạn có thể gửi yêu cầu qua một lệnh gọi lại
để phản hồi thao tác nhấp vào nút hoặc cử chỉ tương tự.
Sau khi bạn gọi
enqueueWork()
,
JobIntentService
thực hiện công việc được xác định trong
onHandleWork()
rồi tự dừng.
Bước tiếp theo là báo cáo kết quả của yêu cầu công việc về Hoạt động gốc
hoặc Mảnh. Bài học tiếp theo sẽ hướng dẫn bạn cách thực hiện việc này bằng
BroadcastReceiver
.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Send work requests to the background service\n\n| **Note:** This page is left here as reference for legacy apps only. See the [guide to background processing on Android](/guide/background) for recommended solutions.\n\n\nThe previous lesson showed you how to create a\n[`JobIntentService`](/reference/androidx/core/app/JobIntentService) class. This\nlesson shows you how to trigger the\n[`JobIntentService`](/reference/androidx/core/app/JobIntentService) to run an operation by\nenqueuing work with an [`Intent`](/reference/android/content/Intent).\nThis [`Intent`](/reference/android/content/Intent) can\noptionally contain data for the\n[`JobIntentService`](/reference/androidx/core/app/JobIntentService) to process.\n\nCreate and send a work request to a JobIntentService\n----------------------------------------------------\n\n\nTo create a work request and send it to a\n[`JobIntentService`](/reference/androidx/core/app/JobIntentService),\ncreate an [`Intent`](/reference/android/content/Intent) and enqueue it to\nbe executed by calling [`enqueueWork()`](/reference/androidx/core/app/JobIntentService#enqueuework).\nOptionally you can add data to the intent (in the form of intent extras) for the\nJobIntentService to process. For more information about creating intents, read the Building an\nintent section in [Intents and Intent Filters](/guide/components/intents-filters)\n\n\nThe following code snippets demonstrate this process:\n\n1. Create a new [`Intent`](/reference/android/content/Intent) for the [`JobIntentService`](/reference/androidx/core/app/JobIntentService) called `RSSPullService`. \n\n ### Kotlin\n\n ```kotlin\n /*\n * Creates a new Intent to start the RSSPullService\n * JobIntentService. Passes a URI in the\n * Intent's \"data\" field.\n */\n serviceIntent = Intent().apply {\n putExtra(\"download_url\", dataUrl)\n }\n ```\n\n ### Java\n\n ```java\n /*\n * Creates a new Intent to start the RSSPullService\n * JobIntentService. Passes a URI in the\n * Intent's \"data\" field.\n */\n serviceIntent = new Intent();\n serviceIntent.putExtra(\"download_url\", dataUrl));\n ```\n2. Call [`enqueueWork()`](/reference/androidx/core/app/JobIntentService#enqueuework) \n\n ### Kotlin\n\n ```kotlin\n private const val RSS_JOB_ID = 1000\n RSSPullService.enqueueWork(context, RSSPullService::class.java, RSS_JOB_ID, serviceIntent)\n ```\n\n ### Java\n\n ```java\n // Starts the JobIntentService\n private static final int RSS_JOB_ID = 1000;\n RSSPullService.enqueueWork(getContext(), RSSPullService.class, RSS_JOB_ID, serviceIntent);\n ```\n\n\nNotice that you can send the work request from anywhere in an Activity or Fragment.\nFor example, if you need to get user input first, you can send the request from a callback\nthat responds to a button click or similar gesture.\n\n\nOnce you call [`enqueueWork()`](/reference/androidx/core/app/JobIntentService#enqueuework),\nthe [`JobIntentService`](/reference/androidx/core/app/JobIntentService) does the work defined in its\n[`onHandleWork()`](/reference/androidx/core/app/JobIntentService#onHandleWork) method, and then stops itself.\n\n\nThe next step is to report the results of the work request back to the originating Activity\nor Fragment. The next lesson shows you how to do this with a\n[BroadcastReceiver](/reference/android/content/BroadcastReceiver)."]]