Android 充电信息的获取【转】
本文转载自:https://blog.csdn.net/wateryi/article/details/50834821
在android系统中,电池信息是由BatteryService.java统一管理的,在BatteryService中,通过:
IBinder b = ServiceManager.getService("batteryproperties");
final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
IBatteryPropertiesRegistrar.Stub.asInterface(b);
try {
batteryPropertiesRegistrar.registerListener(new BatteryListener());
} catch (RemoteException e) {
// Should never happen.
}
向注册回调函数,电池信息一旦有更新,就会调用BatteryListener中的batteryPropertiesChanged接口:
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);
}
}
}
进而调用update函数更新电池信息类:BatteryProperties; 而通常上层接收的电池信息广播也是通过BatteryService中的sendIntentLocked发送的:
private void sendIntentLocked() {
// Pack up the values and broadcast them to everyone
final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
| Intent.FLAG_RECEIVER_REPLACE_PENDING);
int icon = getIconLocked(mBatteryProps.batteryLevel);
intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);
intent.putExtra(BatteryManager.EXTRA_STATUS_SMARTBOOK, mBatteryProps.batteryStatus_smb);
intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth);
intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent);
intent.putExtra(BatteryManager.EXTRA_PRESENT_SMARTBOOK, mBatteryProps.batteryPresent_smb);
intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);
intent.putExtra(BatteryManager.EXTRA_LEVEL_SMARTBOOK, mBatteryProps.batteryLevel_smb);
intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
mHandler.post(new Runnable() {
@Override
public void run() {
ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL);
}
});
}
所以我们上层得到电池信息可以通过广播获取:
private BroadcastReceiver mChargeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int status = intent.getIntExtra("status", 0);
int chargeLevel = intent.getIntExtra("level", 0);
Log.i("wrx_charge", "status = " + status);
Log.i("wrx_charge", "chargeLevel = " + chargeLevel);
String chargeStatus = "";
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_unknow);
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_charging);
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_discharging);
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_notcharging);
break;
case BatteryManager.BATTERY_STATUS_FULL:
chargeStatus = mContext.getString(R.string.bird_keyguard_charge_chargefull);
break;
}
mChargeString = chargeLevel + "% " + chargeStatus;
if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) {
isCharging = true;
} else {
isCharging = false;
}
onChargeChanged();
}
}
};
private void registerChargeReceiver() {
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
getContext().registerReceiver(mChargeReceiver, filter);
}
private void unregisterChargeReceiver() {
getContext().unregisterReceiver(mChargeReceiver);
}
其实手机内部的linux内核的目录下也是有相关电池信息节点的;具体目录如下:
#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"
其中capacity的值就是电池电量的值;我们可以通过以下方式去读取手机相关节点下的值(试过在eng手机中是可以获取到的,不知道user手机会不会存在相关权限问题?):
try {
FileReader localFileReader = new FileReader("文件路径");
BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
str2 = localBufferedReader.readLine();//获取到的文件中的值
localBufferedReader.close();
} catch (IOException e) {
}