上层直接获取系统电量的方法:
BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
所以从BatteryManager.getIntProperty()方法开始吧。来看看getIntProperty的原型。
frameworks/base/core/java/android/os/BatteryManager.java
public int getIntProperty(int id) {
long value = queryProperty(id);
if (value == Long.MIN_VALUE && mContext != null
&& mContext.getApplicationInfo().targetSdkVersion
>= android.os.Build.VERSION_CODES.P) {
return Integer.MIN_VALUE;
}
return (int) value;
}
获取电量,传入的参数是BatteryManager.BATTERY_PROPERTY_CAPACITY
public static final int BATTERY_PROPERTY_CAPACITY = 4;
继续跟queryProperty(id).
/**
* Query a battery property from the batteryproperties service.
*
* Returns the requested value, or Long.MIN_VALUE if property not
* supported on this system or on other error.
*/
private long queryProperty(int id) {
long ret;
if (mBatteryPropertiesRegistrar == null) {
return Long.MIN_VALUE;
}
try {
BatteryProperty prop = new BatteryProperty();
if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)//为0表示读取成功
ret = prop.getLong();//当前电量返回结果
else
ret = Long.MIN_VALUE;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
return ret;
}
private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;
继续看下这里的mBatteryPropertiesRegistrar真身是谁?
// Reduced IBatteryPropertiesRegistrar that only implements getProperty for usage
// in BatteryManager.
private final class BatteryPropertiesRegistrar extends IBatteryPropertiesRegistrar.Stub {
public void registerListener(IBatteryPropertiesListener listener) {
Slog.e(TAG, "health: must not call registerListener on battery properties");
}
public void unregisterListener(IBatteryPropertiesListener listener) {
Slog.e(TAG, "health: must not call unregisterListener on battery properties");
}
public int getProperty(int id, final BatteryProperty prop) throws RemoteException {
traceBegin("HealthGetProperty");
try {
IHealth service = mHealthServiceWrapper.getLastService();
if (service == null) throw new RemoteException("no health service");
final MutableInt outResult = new MutableInt(Result.NOT_SUPPORTED);
switch(id) {
case BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER:
service.getChargeCounter((int result, int value) -> {
outResult.value = result;
if (result == Result.SUCCESS) prop.setLong(value);
});
break;
case BatteryManager.BATTERY_PROPERTY_CURRENT_NOW:
service.getCurrentNow((int result, int value) -> {
outResult.value = result;
if (result == Result.SUCCESS) prop.setLong(value);
});
break;
case BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE:
service.getCurrentAverage((int result, int value) -> {
outResult.value = result;
if (result == Result.SUCCESS) prop.setLong(value);
});
break;
case BatteryManager.BATTERY_PROPERTY_CAPACITY://here we are
service.getCapacity((int result, int value) -> {
outResult.value = result;
if (result == Result.SUCCESS) prop.setLong(value);
});
break;
case BatteryManager.BATTERY_PROPERTY_STATUS:
service.getChargeStatus((int result, int value) -> {
outResult.value = result;
if (result == Result.SUCCESS) prop.setLong(value);
});
break;
case BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER:
service.getEnergyCounter((int result, long value) -> {
outResult.value = result;
if (result == Result.SUCCESS) prop.setLong(value);
});
break;
}
return outResult.value;
} finally {
traceEnd();
}
}
这里的service其实就是HIDL了。
import android.hardware.health.V2_0.IHealth;
IHealth service = mHealthServiceWrapper.getLastService();
下一篇文章将会介绍getCapacity在HIDL中的具体实现