blob: 807e343bd795af571af54488f22b1dbe32f9f34f [file] [log] [blame]
<html devsite><head>
<meta name="book_path" value="/_book.yaml"/>
<meta name="project_path" value="/_project.yaml"/>
</head>
<body>
<!--
Copyright 2018 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<h1 id="supporting_batteryless_devices" class="page-title">支持无电池设备</h1>
<p>本页将介绍 Android 如何应对具有可拆卸电池或无内置电池的产品。无内置电池设备会改为连接到外部电源,例如其他设备上的交流电源插座或 USB 端口。</p>
<h2 id="is_a_battery_present">是否有电池?</h2>
<p>应用可以使用以下代码来检测设备当前是否有电池:</p>
<pre class="prettyprint"><code>```
final Intent batteryInfo = registerReceiver(null, new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
```
</code></pre>
<h2 id="batteryless_device_behavior">无电池设备行为</h2>
<p>如果 Android 未检测出产品的电池设备,则会使用以下与电池相关的默认值。请注意,在 Android 9 版本中,默认值发生了变化。下表显示了不同之处。</p>
<table>
<tbody><tr>
<th>电池状态</th>
<th>Android 9 及更高版本</th>
<th>Android 8.1 及更低版本</th>
</tr>
<tr>
<td><em>存在</em>
</td>
<td>false</td>
<td>true</td>
</tr>
<tr>
<td><em>状态</em>
</td>
<td>未知</td>
<td>正在充电</td>
</tr>
<tr>
<td><em>剩余电量</em>
</td>
<td>0</td>
<td>100%</td>
</tr>
<tr>
<td><em>运行状况</em>
</td>
<td>未知</td>
<td>良好</td>
</tr>
<tr>
<td><em>交流充电器在线状态</em>
</td>
<td>未修改</td>
<td>强制为 true</td>
</tr>
</tbody></table>
<p>制造商可以使用内核 <a href="https://www.kernel.org/doc/Documentation/power/power_supply_class.txt" class="external">power_supply</a> 驱动程序或 <a href="/devices/tech/health/">Health HAL</a> 更改默认设置。</p>
<h3 id="android_and_higher">Android 9 及更高版本</h3>
<p>Android 9 针对无电池设备移除了一些以前的代码;这类设备在默认情况下假定电池存在,充电率为 100%,运行状况良好,且热敏电阻温度读数正常。</p>
<p>处理这项信息的大多数框架 API 会继续像以前那样处理常见情况:系统将被视为“正在充电”<em></em>(即未使用电池电量运行),且不会被视为电量不足。如果界面上显示有电池图标,则它会附带一个感叹号,电量百分比将显示为 0%。但是设备不会因电池电量不足而关闭,而且需要充电或需要电池电量充足的作业也会予以安排。</p>
<h3 id="android_81_and_lower">Android 8.1 及更低版本</h3>
<p>由于电池状态未知,Android 框架 API 会认为系统“正在充电”<em></em>(即未使用电池电量运行),而且不会认为电量不足。如果界面上显示有电池图标,则它会附带一个感叹号,电量百分比将显示为 0%。但是设备不会因电池电量不足而关闭,而且需要充电或需要电池电量充足的作业也会予以安排。</p>
<h2 id="implementation">实现</h2>
<p>如上所述,Android 9 原始代码可在您的设备上正常运行,但建议您对内核或 HAL 进行更改,以准确反映产品的电源和电池状态。如果 Android 9 及更高版本未检测到 <a href="https://www.kernel.org/doc/Documentation/power/power_supply_class.txt" class="external">Linux power supply class</a> 充电器设备,则在默认情况下,所有充电器类型(交流、USB、无线)的状态都将为“离线”<em></em>。如前所述,如果所有充电器都处于离线状态,但未检测到电池设备,则系统仍会被视为“正在充电”,也就是说,系统正在使用外部电源(而不是电池电量)运行。</p>
<p><em></em>如果您的产品没有电池,且会始终连接到电源,那么,您最好针对交流电源或 USB 电源(其在线 <code>sysfs</code> 属性会设置为 <code>true</code>)实现 Linux 内核 power_supply class<em></em> 充电器驱动程序。或者,您也可以在 Health HAL 中为设备配置交流充电器在线属性。为此,请按照<a href="/devices/tech/health/implementation">实现 Health 2.0</a> 中的说明实现 Health HAL。</p>
<p>此自定义 Health HAL 会实现 <code>Health::getHealthInfo()</code> 的自定义版本,而该版本会修改 <code>BatteryProperties.chargerAcOnline = true</code> 的值。</p>
<p>首先,请将文件 <code><a href="https://android.googlesource.com/platform/hardware/interfaces/+/master/health/2.0/default/Health.cpp">hardware/interfaces/health/2.0/default/Health.cpp</a></code> 复制到您自己的 Health HAL 实现,并根据 <a href="https://android.googlesource.com/platform/hardware/interfaces/+/master/health/2.0/README">Health 2.0 README</a> 对其进行修改。</p>
</body></html>