Android 原生设置电池容量是在 power_profile.xml 中配置,此文件默认在 frameworks 目录下,也可能有 overlay 目录文件。
<!-- This is the battery capacity in mAh (measured at nominal voltage) --><item name="battery.capacity">1000</item>
在系统 frameworks/base/core/java/com/android/internal/os/PowerProfile.java 中包含电池相关信息接口,创建 PowerProfile 对象时,会加载保存 power_profile.xml 中配置的值。根据 xml item 类型分别保存在 sPowerItemMap 及 sPowerArrayMap 中。三方应用通过调用 getAveragePowerOrDefault 方法获取对应值。
/*** Returns the average current in mA consumed by the subsystem, or the given* default value if the subsystem has no recorded value.** @param type the subsystem type* @param defaultValue the value to return if the subsystem has no recorded value.* @return the average current in milliAmps.*/public double getAveragePowerOrDefault(String type, double defaultValue) {if (sPowerItemMap.containsKey(type)) {return sPowerItemMap.get(type);} else if (sPowerArrayMap.containsKey(type)) {return sPowerArrayMap.get(type)[0];} else {return defaultValue;}}
原生接口缺陷很明显,就是一个项目只能配置一个固定的电池容量值。如果项目有大小不同容量电池,就需要改动原生接口。通过读取电池节点中值动态获取。节点中值是直接获取电池中信息写入节点的,更准确点。
//电池容量节点
/sys/class/power_supply/battery/charge_full_design