Android系统开发--Android灯光系统之电池灯的流程分析
前期系统准备
-
运行初始化,创建系统服务
-
创建电池服务,获得电池灯;创建监听者监听上报电池事件;
-
mSystemServiceManager.startService(BatteryService.class);
-
mLed = new Led(contegetLocalService(LightsManager.class)); //实例化灯对象
-
mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY); //获得电池灯
-
batteryPropertiesRegistrar.registerListener(new BatteryListener());
publishBinderService("battery", new BinderService());
publishLocalService(BatteryManagerInternal.class, new LocalService()); //创建电池状态监听者 -
private final class BatteryListener extends IBatteryPropertiesListener.Stub {
@Override
public void batteryPropertiesChanged(BatteryProperties props) {
final long identity = Binder.clearCallingIdentity(); try { BatteryService.this.update(props); } finally { Binder.restoreCallingIdentity(identity); }
}
-
} //判断是否电池状态是否发生变化,如果发生变化,则将该事件通知
-
创建电量控制服务;获得电池服务;注册电池广播监听;
-
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); //创建电量管理服务
-
mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class); //获取电池服务后通知系统准备就绪 -
注册电池广播接收器
临听ACTION_BATTERY_CHANGED(电池状态改变)
-
驱动上报事件处理流程
-
Linux底层驱动上报电池事件
-
BatteryListener的监听函数batteryPropertiesChanged被调用
-
广播ACTION_BATTERY_CHANGED更新电池灯
-
BatteryService.java:update()
processValuesLocked(false); //通知电池服务更新电池灯
-
sendIntentLocked(); //通知到电池接收广播
-
mLed.updateLightsLocked(); //更新电池灯
-
-
收到广播消息后执行BatteryReceiver.onReceive
-
handleBatteryStateChangedLocked(); //处理电池状态变化
-
PowerManagerService.java: handleBatteryStateChangedLocked()
updatePowerStateLocked(); //更新电源状态
- PowerManagerService.java: updatePowerStateLocked()
updatePowerStateLocked();