自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
動態可用的 HAL
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 9 支援在 Android 硬體子系統未使用或不需要時,動態關閉這些子系統。舉例來說,當使用者未使用 Wi-Fi 時,Wi-Fi 子系統不應占用記憶體、電力或其他系統資源。在舊版 Android 中,HAL/驅動程式會在 Android 手機啟動的整個期間保持開啟狀態。
實作動態關機功能時,需要連結資料流程並執行動態程序,詳情請參閱下文。
HAL 定義的變更
動態關機功能需要哪些程序提供哪些 HAL 介面 (這項資訊日後在其他情境中也可能派上用場) 的資訊,以及在啟動時不啟動程序,並在程序結束時不重新啟動 (直到再次要求為止)。
# 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
對 init 和 hwservicemanager 的異動
動態關機功能也需要 hwservicemanager
告知 init
啟動要求的服務。在 Android 9 中,init
包含三個額外的控制訊息 (例如 ctl.start
):ctl.interface_start
、ctl.interface_stop
和 ctl.interface_restart
。這些訊息可用來傳送信號給 init
,以便開啟或關閉特定硬體介面。當服務要求未註冊時,hwservicemanager
會要求啟動服務。不過,動態 HAL 不需要使用任何這些元素。
判斷 HAL 結束
在 Android 9 中,必須手動決定 HAL 結束。對於 Android 10 以上版本,您也可以使用自動生命週期來判斷。
動態關機功能需要多項政策,才能決定何時啟動 HAL 和關閉 HAL。如果 HAL 因任何原因決定退出,系統會在需要時再次啟動,並使用 HAL 定義中提供的資訊,以及 init
和 hwservicemanager
變更所提供的基礎架構。這可能涉及幾種不同的策略,包括:
- 如果有人在 HAL 上呼叫相似或相近的 API,HAL 可以選擇自行呼叫 exit。您必須在對應的 HAL 介面中指定這項行為。
- HAL 可在工作完成時關閉 (HAL 檔案中會記錄這項資訊)。
自動生命週期
Android 10 為核心和 hwservicemanager
提供更多支援,讓 HAL 在沒有用戶端時自動關閉。如要使用這項功能,請按照「HAL 定義變更」一文中的所有步驟操作,並完成以下步驟:
- 請使用
LazyServiceRegistrar
而非成員函式 registerAsService
,在 C++ 中註冊服務,例如:// only one instance of LazyServiceRegistrar per process
LazyServiceRegistrar registrar;
registrar.registerAsService(myHidlService /* , "default" */);
- 請確認 HAL 用戶端只在使用時保留頂層 HAL (使用
hwservicemanager
註冊的介面) 的參照。為避免在繼續執行的 hwbinder 執行緒上放棄此參照而造成延遲,用戶端應在放棄參照後呼叫 IPCThreadState::self()->flushCommands()
,以確保繫結器驅動程式收到相關參照計數變更的通知。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","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-26 (世界標準時間)。"],[],[],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."]]