在SystemServer.java中可以看到启动BatteryService的代码:


          Log.i(TAG, \\\"Starting Battery Service.\\\");
         BatteryService battery = new BatteryService(context);
         ServiceManager.addService(\\\"battery\\\", battery);


========================================================================================
1. 数据来源
       BatteryService通过JNI(com_android_server_BatteryService.cpp)读取数据。BatteryService通过JNI注册的不仅有函数,还有变量。 如下:


   //在BatteryService.java中声明的变量,在com_android_server_BatteryService.cpp中共用,即在com_android_server_BatteryService.cpp中其实操作的也是BatteryService.java中声明的变量


   gFieldIds.mAcOnline = env->GetFieldID(clazz, \\\"mAcOnline\\\", \\\"Z\\\"); 
   gFieldIds.mUsbOnline = env->GetFieldID(clazz, \\\"mUsbOnline\\\", \\\"Z\\\"); 
   gFieldIds.mBatteryStatus = env->GetFieldID(clazz, \\\"mBatteryStatus\\\", \\\"I\\\"); 
   gFieldIds.mBatteryHealth = env->GetFieldID(clazz, \\\"mBatteryHealth\\\", \\\"I\\\"); 
   gFieldIds.mBatteryPresent = env->GetFieldID(clazz, \\\"mBatteryPresent\\\", \\\"Z\\\"); 
   gFieldIds.mBatteryLevel = env->GetFieldID(clazz, \\\"mBatteryLevel\\\", \\\"I\\\"); 
   gFieldIds.mBatteryTechnology = env->GetFieldID(clazz, \\\"mBatteryTechnology\\\", \\\"Ljava/lang/String;\\\"); 
   gFieldIds.mBatteryVoltage = env->GetFieldID(clazz, \\\"mBatteryVoltage\\\", \\\"I\\\"); 
   gFieldIds.mBatteryTemperature = env->GetFieldID(clazz, \\\"mBatteryTemperature\\\", \\\"I\\\");


#define AC_ONLINE_PATH "/sys/class/power_supply/ac/online" #define USB_ONLINE_PATH "/sys/class/power_supply/usb/online" #define BATTERY_STATUS_PATH "/sys/class/power_supply/battery/status" #define BATTERY_HEALTH_PATH "/sys/class/power_supply/battery/health" #define BATTERY_PRESENT_PATH "/sys/class/power_supply/battery/present" #define BATTERY_CAPACITY_PATH "/sys/class/power_supply/battery/capacity" #define BATTERY_VOLTAGE_PATH "/sys/class/power_supply/battery/batt_vol" #define BATTERY_TEMPERATURE_PATH "/sys/class/power_supply/battery/batt_temp" #define BATTERY_TECHNOLOGY_PATH "/sys/class/power_supply/battery/technology"
 
  

-----------------------------------------------------------------------------------------------------------------------------------
2. 数据传送
       电池的这些信息是通过何种方式,被其他应用所获得的。可以想到的有两种方式,第一种,应用主动从BatteryService获得数据;第二种,BatteryService主动把数据传送给所关心的应用程序。


----------------------------------------------------------------------------------------------------------------------------------- 
3. 数据接收
应用如果想要接收到BatteryService发送出来的电池信息,则需要注册一个Intent为Intent.ACTION_BATTERY_CHANGED的BroadcastReceiver。



----------------------------------------------------------------------------------------------------------------------------------- 
4. 数据更新
电池的信息会随着时间不停变化,自然地,就需要考虑如何实时的更新电池的数据信息。在BatteryService启动的时候,会同时通过UEventObserver启动一个onUEvent Thread。



     在UEvent thread中会不停调用 update()方法,来更新电池的信息数据。