將工作要求傳送給背景服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
先前的課程介紹了
JobIntentService
類別。這個
本課程將說明如何觸發
JobIntentService
來執行
使用 Intent
將工作排入佇列。
這個Intent
可以
視需要納入
JobIntentService
來處理工作。
建立工作要求並傳送至 JobIntentService
如何建立工作要求並傳送至
JobIntentService
、
建立 Intent
並排入佇列
執行時,系統會呼叫
enqueueWork()
。
您也可以選擇將相關資料新增至意圖 (以意圖額外項目的形式),
要處理的 JobIntentService。如要進一步瞭解如何建立意圖,請參閱
意圖和意圖篩選器中的意圖區段
下列程式碼片段可說明這個程序:
-
為
Intent
JobIntentService
「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));
-
撥打 的電話
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);
請注意,您可以從 Activity 或 Fragment 中的任一處傳送工作要求。
舉例來說,如需先取得使用者輸入內容,您可以透過回呼傳送要求
回應按鈕點選或類似手勢。
當您呼叫 之後
enqueueWork()
,
JobIntentService
會執行其中定義的工作
onHandleWork()
方法,然後自行停止。
下一步是將工作要求的結果回報給原始 Activity
或 Fragment下一堂課將說明如何透過
BroadcastReceiver
。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# 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)."]]