android BatteryManager电池管理

首先要说的是,电池管理不是电源管理。算然电源是由电池供给的,但是在逻辑管理中,怎么供电的,和怎么用电的,这两个是分开的。

java代码: 
frameworks/frameworks/base/services/java/com/android/server/Ba 
tteryService.java 
监听Uevent、读取sysfs里中的状态 。
JNI代码: 
frameworks/base/services/jni/com_android_server_BatteryService. 
cpp 
kernel driver代码 
drivers/power/xx_battery.c 

获得电池信息的两种方式(可以3种):

Android.content.BroadcastReceiver类
广播Intent.ACTION_BATTERY_CHANGED

Android.os.BatteryManager类


1.使用广播接收器:

   ●(1) 电池信息的取得,调用registerReceiver()方法。
         第1个参数,设置BroadcastReceiver实例
第2个参数,设置追加了Intent.ACTION_BATTERY_CHANGED处理的IntentFilter实例。

    ●(2) 在BroadcastReceiver的onReceive()事件,接收到的Intent.ACTION_BATTERY_CHANGED,包括下面的信息。
    “status”(int类型)…状态,定义值是BatteryManager.BATTERY_STATUS_XXX。
    “health”(int类型)…健康,定义值是BatteryManager.BATTERY_HEALTH_XXX。
    “present”(boolean类型)
    “level”(int类型)…电池剩余容量
    “scale”(int类型)…电池最大值。通常为100。
    “icon-small”(int类型)…图标ID。
    “plugged”(int类型)…连接的电源插座,定义值是BatteryManager.BATTERY_PLUGGED_XXX。
    “voltage”(int类型)…mV。
    “temperature”(int类型)…温度,0.1度单位。例如 表示197的时候,意思为19.7度。 
    “technology”(String类型)…电池类型,例如,Li-ion等等。

2.获取系统服务的方式:


Constants
int BATTERY_HEALTH_COLD  
int BATTERY_HEALTH_DEAD  
int BATTERY_HEALTH_GOOD  
int BATTERY_HEALTH_OVERHEAT  
int BATTERY_HEALTH_OVER_VOLTAGE  
int BATTERY_HEALTH_UNKNOWN  
int BATTERY_HEALTH_UNSPECIFIED_FAILURE  
int BATTERY_PLUGGED_AC Power source is an AC charger.
int BATTERY_PLUGGED_USB Power source is a USB port.
int BATTERY_PLUGGED_WIRELESS Power source is wireless.
int BATTERY_STATUS_CHARGING  
int BATTERY_STATUS_DISCHARGING  
int BATTERY_STATUS_FULL  
int BATTERY_STATUS_NOT_CHARGING  
int BATTERY_STATUS_UNKNOWN  
String EXTRA_HEALTH Extra for ACTION_BATTERY_CHANGED: integer containing the current health constant.
String EXTRA_ICON_SMALL Extra for ACTION_BATTERY_CHANGED: integer containing the resource ID of a small status bar icon indicating the current battery state.
String EXTRA_LEVEL Extra for ACTION_BATTERY_CHANGED: integer field containing the current battery level, from 0 to EXTRA_SCALE.
String EXTRA_PLUGGED Extra for ACTION_BATTERY_CHANGED: integer indicating whether the device is plugged in to a power source; 0 means it is on battery, other constants are different types of power sources.
String EXTRA_PRESENT Extra for ACTION_BATTERY_CHANGED: boolean indicating whether a battery is present.
String EXTRA_SCALE Extra for ACTION_BATTERY_CHANGED: integer containing the maximum battery level.
String EXTRA_STATUS Extra for ACTION_BATTERY_CHANGED: integer containing the current status constant.
String EXTRA_TECHNOLOGY Extra for ACTION_BATTERY_CHANGED: String describing the technology of the current battery.
String EXTRA_TEMPERATURE Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.
String EXTRA_VOLTAGE Extra for ACTION_BATTERY_CHANGED: integer containing the current battery voltage level.

你可能感兴趣的:(AndroidOS)