android 9.0 getIntProperty获取系统当前电量流程分析(一)

本文详细解析了在Android 9.0中通过BatteryManager获取设备当前电量的内部流程,从上层调用getIntProperty方法开始,深入探讨了其如何通过HIDL与健康服务(Health Service)交互,最终获取电池容量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上层直接获取系统电量的方法:

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中的具体实现

android 9.0 getIntProperty获取系统当前电量流程分析(二)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值