建立背景服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
IntentService
類別提供簡單明瞭的執行結構
在單一背景執行緒上處理一項作業以便處理長時間執行的作業
且不會影響使用者介面的反應速度此外,
IntentService
未受大部分的使用者介面生命週期事件影響,因此
在停止 AsyncTask
的情況下繼續執行
IntentService
具有下列限制:
不過在大多數情況下,建議您使用 IntentService
執行
簡單的背景作業
本指南說明如何執行下列操作:
處理傳入意圖
如要為應用程式建立 IntentService
元件,請定義會用來
可擴充 IntentService
,然後在當中定義
覆寫 onHandleIntent()
。例如:
Kotlin
class RSSPullService : IntentService(RSSPullService::class.simpleName)
override fun onHandleIntent(workIntent: Intent) {
// Gets data from the incoming Intent
val dataString = workIntent.dataString
...
// Do work here, based on the contents of dataString
...
}
}
Java
public class RSSPullService extends IntentService {
@Override
protected void onHandleIntent(Intent workIntent) {
// Gets data from the incoming Intent
String dataString = workIntent.getDataString();
...
// Do work here, based on the contents of dataString
...
}
}
請注意,一般 Service
元件的其他回呼,例如
onStartCommand()
會自動叫用
IntentService
。在 IntentService
中,應避免
以覆寫這些回呼
如要進一步瞭解如何建立 IntentService
,請參閱擴充
IntentService 類別。
在資訊清單中定義意圖服務
IntentService
也需要應用程式資訊清單中的項目。
以此身分提供這個項目
<service>
該元素的子項
<application>
元素:
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
...
<!--
Because android:exported is set to "false",
the service is only available to this app.
-->
<service
android:name=".RSSPullService"
android:exported="false"/>
...
</application>
android:name
屬性會指定
IntentService
。
請注意,
<service>
元素並未包含
「意圖篩選器」。
如果 Activity
將工作要求傳送至服務,就會使用
明確 Intent
,因此不必使用篩選器。這也
換言之,只有相同應用程式或其他應用程式中的元件,具有
才能存取服務
您已瞭解基本的 IntentService
類別,現在可以傳送工作要求了
加入 Intent
物件建構這些物件的程序
以及如何將這類電子郵件傳送到您的 IntentService
,詳情請參閱
將工作要求傳送至背景服務。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Create a background service\n\n| **Note:** `IntentService` will not work correctly when the application is in the background on the latest versions of Android. This page is left here as reference for legacy apps only. See the [guide to background processing\n| on Android](/guide/background) for recommended solutions.\n\n\nThe [IntentService](/reference/android/app/IntentService) class provides a straightforward structure for running\nan operation on a single background thread. This allows it to handle long-running operations\nwithout affecting your user interface's responsiveness. Also, an\n[IntentService](/reference/android/app/IntentService) isn't affected by most user interface lifecycle events, so it\ncontinues to run in circumstances that would shut down an [AsyncTask](/reference/android/os/AsyncTask)\n\n\nAn [IntentService](/reference/android/app/IntentService) has a few limitations:\n\n- It can't interact directly with your user interface. To put its results in the UI, you have to send them to an [Activity](/reference/android/app/Activity).\n- Work requests run sequentially. If an operation is running in an [IntentService](/reference/android/app/IntentService), and you send it another request, the request waits until the first operation is finished.\n- An operation running on an [IntentService](/reference/android/app/IntentService) can't be interrupted.\n\n\nHowever, in most cases an [IntentService](/reference/android/app/IntentService) is the preferred way to perform\nsimple background operations.\n\n\nThis guide shows you how to do the following things:\n\n- Create your own subclass of [IntentService](/reference/android/app/IntentService).\n- Create the required callback method [onHandleIntent()](/reference/android/app/IntentService#onHandleIntent(android.content.Intent)).\n- Define the [IntentService](/reference/android/app/IntentService) in your manifest file.\n\nHandle incoming intents\n-----------------------\n\n\nTo create an [IntentService](/reference/android/app/IntentService) component for your app, define a class that\nextends [IntentService](/reference/android/app/IntentService), and within it, define a method that\noverrides [onHandleIntent()](/reference/android/app/IntentService#onHandleIntent(android.content.Intent)). For example: \n\n### Kotlin\n\n```kotlin\nclass RSSPullService : IntentService(RSSPullService::class.simpleName)\n\n override fun onHandleIntent(workIntent: Intent) {\n // Gets data from the incoming Intent\n val dataString = workIntent.dataString\n ...\n // Do work here, based on the contents of dataString\n ...\n }\n}\n```\n\n### Java\n\n```java\npublic class RSSPullService extends IntentService {\n @Override\n protected void onHandleIntent(Intent workIntent) {\n // Gets data from the incoming Intent\n String dataString = workIntent.getDataString();\n ...\n // Do work here, based on the contents of dataString\n ...\n }\n}\n```\n\n\nNotice that the other callbacks of a regular [Service](/reference/android/app/Service) component, such as\n[onStartCommand()](/reference/android/app/Service#onStartCommand(android.content.Intent, int, int)) are automatically invoked by\n[IntentService](/reference/android/app/IntentService). In an [IntentService](/reference/android/app/IntentService), you should avoid\noverriding these callbacks.\n\nTo learn more about creating an `IntentService`, see [Extending the\nIntentService class](/guide/components/services#ExtendingIntentService).\n\nDefine the intent service in the manifest\n-----------------------------------------\n\n\nAn [IntentService](/reference/android/app/IntentService) also needs an entry in your application manifest.\nProvide this entry as a\n[\u003cservice\u003e](/guide/topics/manifest/service-element)\nelement that's a child of the\n[\u003capplication\u003e](/guide/topics/manifest/application-element) element: \n\n```xml\n \u003capplication\n android:icon=\"@drawable/icon\"\n android:label=\"@string/app_name\"\u003e\n ...\n \u003c!--\n Because android:exported is set to \"false\",\n the service is only available to this app.\n --\u003e\n \u003cservice\n android:name=\".RSSPullService\"\n android:exported=\"false\"/\u003e\n ...\n \u003c/application\u003e\n```\n\n\nThe attribute `android:name` specifies the class name of the\n[IntentService](/reference/android/app/IntentService).\n\n\nNotice that the\n[\u003cservice\u003e](/guide/topics/manifest/service-element)\nelement doesn't contain an\n[intent filter](/guide/components/intents-filters). The\n[Activity](/reference/android/app/Activity) that sends work requests to the service uses an\nexplicit [Intent](/reference/android/content/Intent), so no filter is needed. This also\nmeans that only components in the same app or other applications with the\nsame user ID can access the service.\n\n\nNow that you have the basic [IntentService](/reference/android/app/IntentService) class, you can send work requests\nto it with [Intent](/reference/android/content/Intent) objects. The procedure for constructing these objects\nand sending them to your [IntentService](/reference/android/app/IntentService) is described in\n[Send work requests to the background service](/training/run-background-service/send-request)."]]