自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
支持无电池设备
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页将介绍 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 及更低版本
|
存在
|
false
|
true
|
状态
|
未知
|
正在充电
|
剩余电量
|
0
|
100%
|
运行状况
|
未知
|
良好
|
交流充电器在线状态
|
未修改
|
强制为 true |
制造商可以使用内核 power_supply 驱动程序或 Health HAL 更改默认设置。
Android 9 及更高版本
Android 9 针对无电池设备移除了一些以前的代码;这类设备在默认情况下假定电池存在,充电率为 100%,运行状况良好,且热敏电阻温度读数正常。
处理这项信息的大多数框架 API 会继续像以前那样处理常见情况:系统将被视为“正在充电”(即未使用电池电量运行),且不会被视为电量不足。如果界面上显示有电池图标,则它会附带一个英文感叹号,电量百分比将显示为 0%。但是设备不会因电池电量不足而关闭,而且需要充电或需要电池电量充足的作业也会予以安排。
Android 8.1 及更低版本
由于电池状态未知,Android 框架 API 会认为系统“正在充电”(即未使用电池电量运行),而且不会认为电量不足。如果界面上显示有电池图标,则它会附带一个感叹号,电量百分比将显示为 0%。但是设备不会因电池电量不足而关闭,而且需要充电或需要电池电量充足的作业也会予以安排。
实现
如上所述,Android 9 原始代码可在您的设备上正常运行,但建议您对内核或 HAL 进行更改,以准确反映产品的电源和电池状态。如果 Android 9 及更高版本未检测到 Linux power supply 类充电器设备,在默认情况下,所有充电器类型(交流、USB、无线)的状态都将为“离线”。如前所述,如果所有充电器都处于离线状态,但未检测到电池设备,系统仍会被视为“正在充电”,也就是说,系统正在使用外部电源(而不是电池电量)运行。
如果您的产品没有电池,且始终连接到电源,那么,您最好针对交流电源或 USB 电源(其在线 sysfs
属性会设置为 true
)实现 Linux 内核 power_supply 类充电器驱动程序。或者,您也可以在 Health HAL 中为设备配置交流充电器在线属性。为此,请按照实现 Health 2.0 中的说明实现 Health HAL。
此自定义 Health HAL 会实现 Health::getHealthInfo()
的自定义版本,而该版本会修改 BatteryProperties.chargerAcOnline = true
的值。
首先,请将文件 hardware/interfaces/health/2.0/default/Health.cpp
复制到您自己的 Health HAL 实现中,并根据 Health 2.0 README 对其进行修改。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-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"]],["最后更新时间 (UTC):2025-03-26。"],[],[],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)."]]