8.4 Android灯光系统_源码分析_电池灯

电池灯的Java代码在batteryservice.java中

电池的状态电量等信息由驱动获得,但驱动不会主动做这些事情,因此肯定有个App调用驱动程序读取电池信息,称这个App为A应用。

还有个App应用B,根据电量低的情况提示对话框:提示充电、对话框:将在30S内自动关机,应用A会发出一些通知 

说明:

(1)APP0:读取驱动,当驱动检测到电池事件发生,唤醒APP0

(2)APP1,2,3......  注册通知响应函数

(3)APP0发出通知

(4)APP1,2,3.....执行对应的函数

Ctrl+Shift+N 输入batteryservice.java打卡

//首先在onStart()函数中注册一个监听事件,在BatteryListener类中,如果电池属性发生改变,batteryPropertiesChanged方法会被调用

a. batteryPropertiesRegistrar.registerListener(new BatteryListener());

//batteryPropertiesChanged方法会调用update方法,在update中广播消息,消息属性是Intent.ACTION_BATTERY_CHANGED,在android源码中搜索,其在很多地方被用到,比如PowerManagerService.java中,其会注册一个接受者
b. sendIntentLocked();

//App0发出广播消息后会继续调用灯光的updateLightsLocked函数
c. mLed.updateLightsLocked();

//App1/2/3注册接受者,等待消息,收到感兴趣的Intent后,调用BatteryReceiver类中的onReceive函数
d.
// Register for broadcasts from other components of the system.
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
mContext.registerReceiver(new BatteryReceiver(), filter, null, mHandler);

//App1/2/3收到消息后调用如下内容

e.
onReceive
  handleBatteryStateChangedLocked
    updatePowerStateLocked

参考文章
Android4.4电池管理
http://blog.csdn.net/wlwl0071986/article/details/38778897

原文地址:https://www.cnblogs.com/liusiluandzhangkun/p/9130671.html