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.
HAL có sẵn động
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.
Android 9 hỗ trợ tắt động các hệ thống con phần cứng Android khi không sử dụng hoặc không cần thiết. Ví dụ: khi người dùng không sử dụng Wi-Fi, các hệ thống con Wi-Fi không được chiếm dụng bộ nhớ, nguồn điện hoặc các tài nguyên hệ thống khác. Trong các phiên bản Android trước, HAL/trình điều khiển được giữ mở trên các thiết bị Android trong toàn bộ thời gian khởi động điện thoại Android.
Việc triển khai tắt động liên quan đến việc kết nối các luồng dữ liệu và thực thi các quy trình động như được nêu chi tiết trong các phần sau.
Thay đổi đối với định nghĩa HAL
Tắt động yêu cầu thông tin về những quy trình nào phục vụ giao diện HAL nào (thông tin này cũng có thể hữu ích sau này trong các ngữ cảnh khác) cũng như không khởi động các quy trình khi khởi động và không khởi động lại các quy trình đó (cho đến khi được yêu cầu lại) khi các quy trình đó thoát.
# some init.rc script associated with the HAL
service vendor.some-service-name /vendor/bin/hw/some-binary-service
# init language extension, provides information of what service is served
# if multiple interfaces are served, they can be specified one on each line
interface android.hardware.light@2.0::ILight default
# restarted if hwservicemanager dies
# would also cause the hal to start early during boot if disabled wasn't set
class hal
# will not be restarted if it exits until it is requested to be restarted
oneshot
# will only be started when requested
disabled
# ... other properties
Thay đổi đối với init và hwservicemanager
Tắt động cũng yêu cầu hwservicemanager
cho init
biết để bắt đầu các dịch vụ được yêu cầu. Trong Android 9, init
bao gồm 3 thông báo điều khiển bổ sung (ví dụ: ctl.start
): ctl.interface_start
, ctl.interface_stop
và ctl.interface_restart
.
Bạn có thể dùng các thông báo này để báo hiệu cho init
hiển thị và ẩn các giao diện phần cứng cụ thể. Khi một dịch vụ được yêu cầu và chưa được đăng ký, hwservicemanager
sẽ yêu cầu bắt đầu dịch vụ đó. Tuy nhiên, HAL động không yêu cầu sử dụng bất kỳ phương thức nào trong số này.
Xác định lối thoát HAL
Trong Android 9, bạn phải xác định chế độ thoát HAL theo cách thủ công. Đối với Android 10 trở lên, bạn cũng có thể xác định thời điểm này bằng vòng đời tự động.
Tắt động yêu cầu nhiều chính sách để quyết định thời điểm bắt đầu và tắt HAL. Nếu một HAL quyết định thoát vì bất kỳ lý do gì, HAL đó sẽ tự động khởi động lại khi cần thiết bằng cách sử dụng thông tin được cung cấp trong định nghĩa HAL và cơ sở hạ tầng do các thay đổi đối với init
và hwservicemanager
cung cấp. Việc này có thể liên quan đến một số chiến lược, bao gồm:
- HAL có thể chọn tự gọi lệnh thoát nếu có người gọi một API đóng hoặc tương tự trên đó. Bạn phải chỉ định hành vi này trong giao diện HAL tương ứng.
- HAL có thể tắt khi hoàn thành tác vụ (được ghi lại trong tệp HAL).
Vòng đời tự động
Android 10 bổ sung thêm tính năng hỗ trợ cho nhân và hwservicemanager
, cho phép HAL tự động tắt bất cứ khi nào không có ứng dụng nào. Để sử dụng tính năng này, hãy thực hiện tất cả các bước trong phần Thay đổi đối với định nghĩa HAL cũng như:
- Đăng ký dịch vụ trong C++ bằng
LazyServiceRegistrar
thay vì hàm thành viên registerAsService
, ví dụ:
// only one instance of LazyServiceRegistrar per process
LazyServiceRegistrar registrar;
registrar.registerAsService(myHidlService /* , "default" */);
- Xác minh rằng ứng dụng HAL chỉ giữ lại tệp tham chiếu đến HAL cấp cao nhất (giao diện được đăng ký bằng
hwservicemanager
) khi ứng dụng đó đang được sử dụng. Để tránh bị chậm trễ nếu tệp đối chiếu này bị loại bỏ trên luồng hwbinder tiếp tục thực thi, ứng dụng cũng phải gọi IPCThreadState::self()->flushCommands()
sau khi loại bỏ tệp đối chiếu để đảm bảo trình điều khiển liên kết được thông báo về các thay đổi liên quan đến số lượng tệp đối chiếu.
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-07-26 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-07-26 UTC."],[],[],null,["# Dynamically available HALs\n\nAndroid 9 supports the dynamic shutdown of Android hardware subsystems when they are not in use or not needed. For example, when a user is not using Wi-Fi, the Wi-Fi subsystems should not be taking up memory, power, or other system resources. In earlier versions of Android, HALs/drivers were kept open on Android devices for the entire duration an Android phone was booted.\n\nImplementing dynamic shutdown involves wiring up data flows and executing\ndynamic processes as detailed in the following sections.\n\nChanges to HAL definitions\n--------------------------\n\nDynamic shutdown requires information on which processes serve what HAL\ninterfaces (this information may also be useful later in other contexts) as\nwell as not starting processes on boot and not restarting them (until\nrequested again) when they exit. \n\n```objective-c\n# some init.rc script associated with the HAL\nservice vendor.some-service-name /vendor/bin/hw/some-binary-service\n # init language extension, provides information of what service is served\n # if multiple interfaces are served, they can be specified one on each line\n interface android.hardware.light@2.0::ILight default\n # restarted if hwservicemanager dies\n # would also cause the hal to start early during boot if disabled wasn't set\n class hal\n # will not be restarted if it exits until it is requested to be restarted\n oneshot\n # will only be started when requested\n disabled\n # ... other properties\n```\n\nChanges to init and hwservicemanager\n------------------------------------\n\nDynamic shutdown also requires the `hwservicemanager` to tell\n`init` to start requested services. In Android 9,\n`init` includes three additional control messages (e.g.\n`ctl.start`): `ctl.interface_start`,\n`ctl.interface_stop`, and `ctl.interface_restart`.\nThese messages can be used to signal `init` to bring up and down\nspecific hardware interfaces. When a service is requested and isn't\nregistered, `hwservicemanager` requests that the service be\nstarted. However, dynamic HALs don't require using any of these.\n\nDetermine HAL exit\n------------------\n\nIn Android 9, HAL exit has to be manually\ndetermined. For Android 10 and higher, it can also\nbe determined with\n[automatic lifecycles](#automatic-lifecycles).\n\nDynamic shutdown requires multiple policies for deciding when to start a\nHAL and when to shutdown a HAL. If a HAL decides to exit for any reason, it\nwill automatically be restarted when it is needed again using the information\nprovided in the HAL definition and the infrastructure provided by changes to\n`init` and `hwservicemanager`. This could involve a\ncouple of different strategies, including:\n\n- A HAL could choose to call exit on itself if someone calls a close or similar API on it. This behavior must be specified in the corresponding HAL interface.\n- HALs can shut down when their task is completed (documented in the HAL file).\n\nAutomatic lifecycles\n--------------------\n\nAndroid 10 adds more support to the kernel and\n`hwservicemanager`, which allows HALs to shut down automatically\nwhenever they have no clients. To use this feature, do all of the steps in\n[Changes to HAL definitions](#changes-HAL-definitions) as well\nas:\n\n- Register the service in C++ with `LazyServiceRegistrar` instead of the member function, `registerAsService`, for example: \n\n ```scilab\n // only one instance of LazyServiceRegistrar per process\n LazyServiceRegistrar registrar;\n registrar.registerAsService(myHidlService /* , \"default\" */);\n ```\n- Verify that the HAL client keeps a reference to the top-level HAL (the interface registered with `hwservicemanager`) only when it's in use. To avoid delays if this reference is dropped on a hwbinder thread that continues to execute, the client should also call `IPCThreadState::self()-\u003eflushCommands()` after dropping the reference to ensure that the binder driver is notified of the associated reference count changes."]]