ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
ภาพรวมของ AIDL
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ภาษาที่ใช้สื่อสารข้อมูลระหว่างคอมโพเนนต์ของ Android (AIDL) เป็นเครื่องมือที่ช่วยให้ผู้ใช้
สามารถแยก IPC ออกไปได้ เมื่อกำหนดอินเทอร์เฟซ (ระบุใน.aidl
ไฟล์) ระบบบิลด์ต่างๆ จะใช้ไบนารี aidl
เพื่อสร้างการเชื่อมโยง C++ หรือ Java
เพื่อให้ใช้อินเทอร์เฟซนี้ในกระบวนการต่างๆ ได้ ไม่ว่าจะมีรันไทม์หรือบิตเนสใดก็ตาม
คุณใช้ AIDL ระหว่างกระบวนการใดก็ได้ใน Android ไม่ว่าจะเป็นระหว่างคอมโพเนนต์ของแพลตฟอร์ม
หรือระหว่างแอป แต่จะไม่ใช้เป็น API สำหรับแอป AIDL อาจใช้
เพื่อติดตั้งใช้งาน SDK API ในแพลตฟอร์ม เช่น แต่ SDK API Surface
จะไม่มี AIDL API โดยตรง ดูเอกสารเกี่ยวกับวิธีใช้ AIDL
ระหว่างแอปโดยตรงได้ที่เอกสารประกอบสำหรับนักพัฒนาแอป Androidที่เกี่ยวข้อง
เมื่อใช้ AIDL ระหว่างคอมโพเนนต์ของแพลตฟอร์มที่อัปเดตแยกกัน เช่น
APEX (เริ่มใน Android 10) หรือ HAL (เริ่มใน
Android 11) จะต้องใช้ระบบการกำหนดเวอร์ชันที่เรียกว่า
Stable AIDL
ตัวอย่าง
ตัวอย่างอินเทอร์เฟซ AIDL มีดังนี้
package my.package;
import my.package.Baz; // defined elsewhere
interface IFoo {
void doFoo(Baz baz);
}
กระบวนการของเซิร์ฟเวอร์จะลงทะเบียนอินเทอร์เฟซและให้บริการการเรียกไปยังอินเทอร์เฟซนั้น ส่วนกระบวนการของไคลเอ็นต์
จะทำการเรียกไปยังอินเทอร์เฟซเหล่านั้น ในหลายกรณี กระบวนการจะทำหน้าที่เป็นทั้งไคลเอ็นต์และเซิร์ฟเวอร์เนื่องจากอาจอ้างอิงอินเทอร์เฟซหลายรายการ ดูรายละเอียดเพิ่มเติมเกี่ยวกับภาษา AIDL ได้ที่ภาษา AIDL ดูรายละเอียดเพิ่มเติม
เกี่ยวกับรันไทม์ต่างๆ ที่พร้อมใช้งานอินเทอร์เฟซเหล่านี้ได้ที่
แบ็กเอนด์ AIDL การประกาศประเภทเหล่านี้
จะเหมือนกับการประกาศคลาสในภาษาที่กำหนดทุกประการ แต่จะ
ทำงานในกระบวนการต่างๆ
วิธีการทำงาน
AIDL ใช้ไดรเวอร์เคอร์เนล Binder เพื่อทำการเรียก เมื่อคุณโทรออก ระบบจะแพ็ก
ตัวระบุเมธอดและออบเจ็กต์ทั้งหมดลงในบัฟเฟอร์และคัดลอกไปยัง
กระบวนการระยะไกลซึ่งมีเธรด Binder รออ่านข้อมูลอยู่ เมื่อ Binder
Thread ได้รับข้อมูลสำหรับการทำธุรกรรมแล้ว Thread จะค้นหาออบเจ็กต์ Stub ดั้งเดิม
ในกระบวนการภายใน และคลาสนี้จะคลายแพ็กข้อมูลและเรียกใช้
ออบเจ็กต์อินเทอร์เฟซภายใน ออบเจ็กต์อินเทอร์เฟซภายในนี้คือออบเจ็กต์ที่กระบวนการเซิร์ฟเวอร์
สร้างและลงทะเบียน เมื่อมีการเรียกใช้ในกระบวนการเดียวกันและแบ็กเอนด์เดียวกัน จะไม่มีออบเจ็กต์พร็อกซี ดังนั้นการเรียกใช้จึงเป็นแบบโดยตรงโดยไม่มีการแพ็กหรือแกะแพ็ก ดูข้อมูลเพิ่มเติมได้ที่ภาพรวมของ Binder
โต้ตอบกับบริการในอุปกรณ์
Android มาพร้อมกับคำสั่งบางอย่างที่อนุญาตให้โต้ตอบกับบริการใน
อุปกรณ์ โดยคุณสามารถลองทำสิ่งต่อไปนี้
adb shell dumpsys --help # listing and dumping services
adb shell service --help # sending commands to services for testing
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-30 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-30 UTC"],[],[],null,["# AIDL overview\n\nThe Android Interface Definition Language (AIDL) is a tool that lets users\nabstract away IPC. Given an interface (specified in a `.aidl`\nfile), various build systems use the `aidl` binary to construct C++ or Java\nbindings so that this interface can be used across processes, regardless of the\nruntime or bitness there.\n\nAIDL can be used between any process in Android: between platform components\nor between apps. However, it is never used as an API for apps. AIDL may be used\nto implement an SDK API in the platform, for example, but the SDK API surface\nnever contains AIDL APIs directly. For documentation about how to use AIDL\nbetween apps directly, see corresponding\n[Android developers\ndocumentation](https://developer.android.com/guide/components/aidl).\nWhen AIDL is used between platform components that are updated separately, such\nas APEXes (starting in Android 10) or HALs (starting in\nAndroid 11), the versioning system known as\n[Stable AIDL](/docs/core/architecture/aidl/stable-aidl) must be used.\n\nExample\n-------\n\nHere is an example AIDL interface: \n\n package my.package;\n\n import my.package.Baz; // defined elsewhere\n\n interface IFoo {\n void doFoo(Baz baz);\n }\n\nA *server* process registers an interface and serves calls to it, and a *client*\nprocess makes calls to those interfaces. In many cases, a process acts as both a\nclient and a server since it may be referencing multiple interfaces. For more\ndetails about the AIDL language, see\n[AIDL language](/docs/core/architecture/aidl/aidl-language). For more details\nabout the various runtimes available to use these interfaces, see\n[AIDL backends](/docs/core/architecture/aidl/aidl-backends). These type\ndeclarations are exactly like a class declaration in a given language, but they\nwork across processes.\n\nHow it works\n------------\n\nAIDL uses the binder kernel driver to make calls. When you make a call, a\nmethod identifier and all of the objects are packed onto a buffer and copied to\na remote process where a binder thread waits to read the data. Once a binder\nthread receives data for a transaction, the thread looks up a native stub object\nin the local process, and this class unpacks the data and makes a call on a\nlocal interface object. This local interface object is the one a server process\ncreates and registers. When calls are made in the same process and the same\nbackend, no proxy objects exist, and so calls are direct without any\npacking or unpacking. For further information, see the\n[Binder overview](/docs/core/architecture/ipc/binder-overview).\n\nInteract with services on the device\n------------------------------------\n\nAndroid comes with a few commands to allow interacting with services on the\ndevice. Try: \n\n adb shell dumpsys --help # listing and dumping services\n adb shell service --help # sending commands to services for testing"]]