自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
支援無電池裝置
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面說明 Android 如何處理具有可拆卸電池或無內建電池的產品。後者則連接至外部電源,例如交流電插座或其他裝置的 USB 連接埠。
裝置是否有電池?
應用程式可使用下列程式碼,偵測裝置是否有電池:
```
final Intent batteryInfo = registerReceiver(null, new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
```
無電池裝置行為
如果 Android 未偵測到產品的電池裝置,則會使用下列電池相關的預設值。請注意,Android 9 版本的預設值已變更。下表列出兩者的差異。
電池狀態
|
Android 9 以上版本
|
Android 8.1 以下版本 |
分享螢幕畫面
|
否
|
是 |
狀態
|
未知
|
充電
|
剩餘容量
|
0
|
100%
|
健康
|
未知
|
良好
|
AC 充電器線上狀態
|
未修改
|
強制設為 true
|
製造商可以使用核心 power_supply 驅動程式或 Health HAL 變更預設設定。
Android 9 以上版本
Android 9 移除了先前針對無電池裝置的程式碼,這些裝置預設會假裝有電池,且電池已充電至 100%,並且電池健康度良好,溫度感測器的溫度讀數正常。
處理這類資訊的大多數架構 API 會繼續處理常見情況,與先前相同:系統會視為正在充電 (也就是未使用電池電力運作),不會視為電池電量不足。如果使用者介面繪製電池圖示,圖示會顯示驚嘆號,電池百分比則會顯示為 0%。但裝置不會因電量不足而關機,且會排定需要充電或電量充足的工作。
Android 8.1 以下版本
由於電池狀態不明,Android 架構 API 會將系統視為「充電中」 (或不使用電池電力運作),而不會視為電池電量不足。如果使用者介面顯示電池圖示,該圖示會加上驚嘆號,電池百分比則會顯示為 0%。但裝置不會因電量不足而關機,且會排定需要充電或電量充足的工作。
實作
Android 9 預設程式碼可能會在您的裝置上正常運作,但建議您如前所述,修改核心或 HAL,以便準確反映產品的電源和電池狀態。如果 Android 9 以上版本未偵測到 Linux 電源供應器類別充電器裝置,則根據預設,所有充電器類型 (AC、USB、無線) 的狀態都會為 離線。如果所有充電器都已離線,但系統未偵測到電池裝置,系統仍會視為正在充電,因為它是使用外部電源而非電池供電,如先前所述。
如果產品沒有電池,且始終連接至電源,建議您為 AC 或 USB 電源實作 Linux 核心 power_supply 類別 charger 驅動程式,將其 online sysfs
屬性設為 true
。或者,您也可以在裝置的 Health HAL 中設定 AC 充電器線上屬性。如要在 Health HAL 中設定 AC 充電器線上屬性,請參閱 Health AIDL HAL。
這個自訂的 Health HAL 實作自訂版本的 Health::getHealthInfo()
,用於修改 BatteryProperties.chargerAcOnline = true
的值。
如要開始使用,請將 hardware/interfaces/health/aidl/default/Health.cpp
檔案複製到您自己的 Health HAL 實作中,並根據 Health AIDL HAL 進行修改。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[],[],null,["# Support batteryless devices\n\nThis page describes how Android handles products that have either removable\nbatteries or no internal batteries. The latter devices are instead connected to\nan external power source, such as an AC power outlet or USB port on another\ndevice.\n\nIs a battery present?\n---------------------\n\nThe following code may be used by applications to detect whether the device has\na battery present: \n\n ```\n final Intent batteryInfo = registerReceiver(null, new\n IntentFilter(Intent.ACTION_BATTERY_CHANGED));\n\n return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);\n ```\n\nBatteryless device behavior\n---------------------------\n\nIf Android does not detect a battery device for your product, then the following\nbattery-related default values are used. Note the defaults have changed in the\nAndroid 9 release. This table shows the differences.\n\n| Battery state | Android 9 and higher | Android 8.1 and lower |\n|----------------------------|----------------------|-----------------------|\n| *Present* | false | true |\n| *Status* | unknown | charging |\n| *Remaining capacity* | 0 | 100% |\n| *Health* | unknown | good |\n| *AC charger online status* | not modified | forced to true |\n\nManufacturers may alter the default settings using a kernel\n[power_supply](https://www.kernel.org/doc/Documentation/power/power_supply_class.txt)\ndriver or [Health HAL](/docs/core/perf/health).\n\n### Android 9 and higher\n\nAndroid 9 removes some previous code for batteryless\ndevices that by default pretended a battery was present, was being charged at\n100%, and was in good health with a normal temperature reading on its\nthermistor.\n\nMost framework APIs that deal with this information continue to handle common\nsituations the same as previously: the system is considered to be\n*charging* (that is, not running on battery power), and won't be considered\nto have a low battery. If the user interface draws the battery icon, it will\nappear with an exclamation point, and battery percentage is shown as 0%.\nBut the device won't shut down due to low battery, and jobs that require\ncharging or good battery are scheduled.\n\n### Android 8.1 and lower\n\nBecause the battery status is unknown, the Android framework APIs will consider\nthe system to be *charging* (or, not running on battery power) and won't be\nconsidered to have a low battery. If the user interface renders the battery\nicon, it will appear with an exclamation point, and battery percentage is\nshown as 0%. But the device won't shut down due to low battery, and jobs that\nrequire charging or good battery are scheduled.\n\nImplementation\n--------------\n\nThe Android 9 default code may work properly for your\ndevice, but it's recommended to make either a kernel or a HAL change to\naccurately reflect the power and battery state for your product, as described\npreviously. If Android 9 and higher doesn't detect a [Linux power supply\nclass](https://www.kernel.org/doc/Documentation/power/power_supply_class.txt)\ncharger device, then by default all charger types (AC, USB, Wireless) will have\nstatus *offline*. If all chargers are offline but there is no battery device\ndetected, the system will still be considered to be charging in the sense that\nit is running on external, not battery power, as described previously.\n\nIf your product doesn't have a battery and is always connected to a power\nsource, it's best to implement a Linux kernel power_supply class *charger*\ndriver for the AC or USB power source that sets its *online* `sysfs` attribute\nto `true`. Or you can configure the AC charger online property in a Health HAL\nfor your device. To configure the AC charger online property in a Health HAL,\nrefer to [Health AIDL HAL](https://android.googlesource.com/platform/hardware/interfaces/+/main/health/aidl/README.md).\n\nThis custom Health HAL implements a custom version of `Health::getHealthInfo()`\nthat modifies the value of `BatteryProperties.chargerAcOnline = true`.\n\nTo get started, copy file\n[`hardware/interfaces/health/aidl/default/Health.cpp`](https://android.googlesource.com/platform/hardware/interfaces/+/main/health/aidl/default/Health.cpp)\nto your own Health HAL implementation and modify it according to the\n[Health AIDL HAL](https://android.googlesource.com/platform/hardware/interfaces/+/main/health/aidl/README.md)."]]