将工作请求发送到后台服务
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
上一课介绍了如何创建
JobIntentService
类。本次
如何触发
JobIntentService
,用于执行以下操作:
使用 Intent
将工作加入队列。
此Intent
可以
(可选)包含
JobIntentService
进行处理。
创建工作请求并将其发送到 JobIntentService
要创建工作请求并将其发送到
JobIntentService
、
创建 Intent
并将其加入队列,
调用
enqueueWork()
。
您可以选择向 intent 添加数据(以 intent extra 的形式)
JobIntentService 进行处理。如需详细了解如何创建 intent,请参阅“构建
intent 部分(intent 和 intent 过滤器)
以下代码段展示了此过程:
-
创建一个新的
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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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)."]]