สร้างบริการในเบื้องหลัง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คลาส IntentService
มีโครงสร้างง่ายๆ สำหรับการวิ่ง
การดำเนินการในชุดข้อความเบื้องหลังเดี่ยว ซึ่งช่วยให้จัดการการดำเนินการที่ใช้เวลานานได้
โดยไม่ส่งผลต่อการตอบสนองของอินเทอร์เฟซผู้ใช้ นอกจากนี้
IntentService
ไม่ได้รับผลกระทบจากเหตุการณ์ส่วนใหญ่ในวงจรของผู้ใช้ ดังนั้น
จะยังคงทำงานต่อไปในสถานการณ์ที่จะทำให้ AsyncTask
หยุดทำงาน
IntentService
มีข้อจำกัดบางอย่างดังนี้
-
โดยจะไม่สามารถโต้ตอบกับอินเทอร์เฟซผู้ใช้ของคุณโดยตรง หากต้องการนำผลการค้นหาไปไว้ใน UI คุณจะต้อง
คุณต้องส่งไปยัง
Activity
-
คำของานจะทำงานตามลำดับ หากมีการดำเนินการใน
IntentService
และคุณส่งคำขออีกครั้ง คำขอจะรอจนถึง
การทำงานแรกเสร็จสิ้นแล้ว
-
การดำเนินการที่ทำงานใน
IntentService
ต้องไม่หยุดชะงัก
แต่ในกรณีส่วนใหญ่ IntentService
เป็นวิธีที่เหมาะสม
การดำเนินงานเบื้องหลังอย่างง่าย
คู่มือนี้จะแสดงวิธีทำสิ่งต่างๆ ต่อไปนี้
จัดการ Intent ขาเข้า
หากต้องการสร้างคอมโพเนนต์ IntentService
สำหรับแอป ให้กำหนดคลาสที่
ขยาย IntentService
และภายใน URL ดังกล่าว ให้กำหนดวิธีการที่
ลบล้าง 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
คุณควรหลีกเลี่ยง
และลบล้าง Callback เหล่านี้
หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับการสร้าง IntentService
โปรดดูที่การขยาย
คลาส IntentService
กำหนดบริการ Intent ในไฟล์ Manifest
IntentService
ต้องมีรายการในไฟล์ Manifest ของแอปพลิเคชัน
ระบุรายการนี้เป็น
<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 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],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)."]]