Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release thay vì aosp-main để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Binder là một hệ thống giao tiếp giữa các quy trình, cho phép hai quy trình trên một thiết bị chạy Android giao tiếp với nhau. Binder cung cấp một phương tiện để thực thi các lệnh gọi hàm trong một quy trình khác hoàn toàn minh bạch đối với phương thức gọi.
Trong ngữ cảnh của binder, quy trình gọi được coi là máy khách và điểm cuối của quy trình này được gọi là binder proxy hay proxy. Ngược lại, quy trình đang được gọi sẽ là máy chủ và điểm cuối của quy trình này được gọi là binder node hoặc node.
Mỗi node có thể hiển thị và triển khai giao diện riêng. Thông qua một proxy, máy khách có thể thực thi các phương thức trên một giao diện node như thể lệnh gọi là một lệnh gọi hàm cục bộ. Ví dụ sau đây cho thấy một phương thức đang được gọi:
int result = someNodeInterface.foo(a, b); // someNodeInterface is a proxy object
Giả sử máy khách gọi foo() đang chạy trong quy trình A và máy chủ triển khai foo() đang chạy trong quy trình B. Hình 1 cho thấy cách lệnh gọi này được thực hiện:
Hình 1. Thực thi lệnh gọi Binder.
Để thực thi một phương thức trong một quy trình khác, như minh hoạ trong hình 1, những việc sau sẽ xảy ra:
Mã máy khách trong quy trình A sẽ gọi mã proxy trong quy trình A. Mã proxy trong quy trình A sẽ tạo một giao dịch chứa các mục sau:
Giá trị nhận dạng của node
Giá trị nhận dạng cho phương thức foo() trên node
Một vùng đệm chứa bản sao của các đối số a và b
Giao dịch được gửi đến trình điều khiển hạt nhân binder.
Trình điều khiển hạt nhân binder xác định rằng quy trình B lưu trữ node.
Hạt nhân sao chép toàn bộ giao dịch vào không gian địa chỉ của quy trình B.
Hạt nhân tìm thấy một luồng trong quy trình B để xử lý giao dịch và truyền giao dịch đó cho luồng này.
Luồng này sẽ giải nén giao dịch, tìm node và gửi giao dịch đến đối tượng node.
Đối tượng node lấy giá trị nhận dạng hàm từ giao dịch, giải nén a và b từ vùng đệm giao dịch, đồng thời lưu trữ a và b trong các biến cục bộ.
Đối tượng node gọi foo(a, b) trên mã máy chủ trong quy trình B.
Kết quả của lệnh gọi được trả về trong một giao dịch phản hồi, được truyền đến trình điều khiển hạt nhân rồi quay lại proxy gọi trong quy trình A.
Proxy sẽ trả về kết quả đó cho phương thức gọi trong quy trình A.
Các trường hợp sử dụng Binder
Bạn có thể dùng Binder trong nhiều trường hợp mà hoạt động giao tiếp giữa phần mềm trong các quy trình khác nhau phải diễn ra. Ví dụ:
Ứng dụng camera sử dụng binder để giao tiếp với máy chủ camera trong một quy trình khác. Sau đó, máy chủ camera sẽ dùng binder để giao tiếp với HAL camera trong một quy trình khác.
Một ứng dụng sử dụng binder để giao tiếp với máy chủ hệ thống trong một quy trình khác. Máy chủ hệ thống sử dụng binder để giao tiếp với các HAL trong các quy trình khác.
Một ứng dụng trong một quy trình sử dụng binder để giao tiếp với một ứng dụng khác trong một quy trình khác.
Trình nền hệ thống chịu trách nhiệm cài đặt, cập nhật và xoá các ứng dụng (installd) sử dụng binder để giao tiếp với trình nền thời gian chạy Android ("artd") nhằm biên dịch các ứng dụng.
AIDL và binder
Sử dụng Ngôn ngữ thiết kế giao diện Android (AIDL) để xác định các giao diện lập trình sử dụng binder cho IPC. Để biết thêm thông tin, hãy xem bài viết Tổng quan về AIDL.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-23 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-23 UTC."],[],[],null,["# Binder overview\n\n*Binder* is a system for interprocess\ncommunication that lets two processes on an Android-powered device\ncommunicate. Binder provides a means to execute function calls in another\nprocess that is completely transparent to the caller.\n\nIn binder terms, the calling process is considered the *client* and its\nendpoint is called the *binder proxy* or *proxy* . Conversely,\nthe process being called is the *server* and its endpoint is called the\n*binder node* or *node*.\n\nEach node can expose and implement its own interface. And, using a proxy, the\nclient can execute methods on a node interface as though invocation was a\nlocal function call. The following example shows a method being invoked: \n\n int result = someNodeInterface.foo(a, b); // someNodeInterface is a proxy object\n\nAssume that the client calling `foo()` is running in process A and the server\nimplementing `foo()` is running in process B. Figure 1 shows how this call is\nexecuted:\n\n**Figure 1.** Binder call execution.\n\nTo execute a method in another process, as shown in figure 1,\nthe following occurs:\n\n1. The client code in process A invokes the proxy code in process A. The proxy code in process A creates a transaction containing the following items:\n - An identifier for the node\n - An identifier for the `foo()` method on the node\n - A buffer containing a copy of the arguments `a` and `b`\n2. The transaction is submitted to the binder kernel driver.\n3. The binder kernel driver determines that process B hosts the node.\n4. The kernel copies the entire transaction into process B's address space.\n5. The kernel finds a thread in process B to handle the transaction and passes the transaction to it.\n6. The thread unpacks the transaction, finds the node, and sends the transaction to the node object.\n7. The node object obtains the function identifier from the transaction, unpacks `a` and `b` from the transaction buffer, and stores `a` and `b` in local variables.\n8. The node object calls `foo(a, b)` on the server code in process B.\n9. The result of the call is returned in a reply transaction, which is passed to the kernel driver and then back to the calling proxy in process A.\n10. The proxy returns that result to the caller in process A.\n\nBinder use cases\n----------------\n\nBinder can be used in a variety of scenarios where communication between\nsoftware in different processes must occur. For example:\n\n- A camera app uses binder to communicate with the camera server in\n another process. The camera server then uses binder to communicate with the\n camera HAL in another process.\n\n- An app uses binder to communicate with a system server in another process. The\n system server uses binder to talk to HALs in other processes.\n\n- An app in one process uses binder to communicate with a different app in\n another process.\n\n- The system daemon responsible for installing, updating, and removing\n apps (`installd`) uses binder to communicate with the Android\n runtime daemon ('artd') to compile apps.\n\nAIDL and binder\n---------------\n\nUse the Android Interface Design Language (AIDL) to define programming\ninterfaces that use binder for IPC. For further information, see the\n[AIDL overview](/docs/core/architecture/aidl).\n| **Note:** HIDL is deprecated; only AIDL is recommended for new implementations."]]