1、获取 Android 设备的电池容量
电池容量的获取主要通过反射 com.android.internal.os.PowerProfile
类,调用其中的 getBatteryCapacity 方法获取。具体方法如下
/**
* 获取电池的容量
*
* @param context
* @return
*/
public static double getBatteryTotal(Context context) {
if (batteryCapacity > 0) {
return batteryCapacity;
}
Object mPowerProfile;
final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
try {
mPowerProfile = Class.forName(POWER_PROFILE_CLASS).getConstructor(Context.class).newInstance(context);
batteryCapacity = (double) Class.forName(POWER_PROFILE_CLASS).getMethod("getBatteryCapacity").invoke(mPowerProfile);
} catch (Exception e) {
e.printStackTrace();
}
return batteryCapacity;
}
2、获取 Android 设备当前电池使用量
当前使用量的获取有两种方式,一种通过 Context.BATTERY_SERVICE 系统的电池服务,另一种是通过广播来获取。
1、系统服务方式
/**
* 获取当前电量百分比
*
* @param context
* @return
*/
public static int getBatteryCurrent(Context context) {
int capacity = 0;
try {
BatteryManager manager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
capacity = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);//当前电量剩余百分比
} catch (Exception e) {
}
return capacity;
}
举一反三,看下系统 BatteryManager 类中其他字段值的获取
public class BatteryManager {
/*
* Battery property identifiers. These must match the values in
* frameworks/native/include/batteryservice/BatteryService.h
*/
/** Battery capacity in microampere-hours, as an integer. */
/** 电池容量,单位微安时 */
public static final int BATTERY_PROPERTY_CHARGE_COUNTER = 1;
/**
* Instantaneous battery current in microamperes, as an integer. Positive
* values indicate net current entering the battery from a charge source,
* negative values indicate net current discharging from the battery.
*/
/** 电池当前电流值(微安),正值表示充电,负值表示放电 */
public static final int BATTERY_PROPERTY_CURRENT_NOW = 2;
/**
* Average battery current in microamperes, as an integer. Positive
* values indicate net current entering the battery from a charge source,
* negative values indicate net current discharging from the battery.
* The time period over which the average is computed may depend on the
* fuel gauge hardware and its configuration.
*/
/** 平均电流(微安)*/
public static final int BATTERY_PROPERTY_CURRENT_AVERAGE = 3;
/**
* Remaining battery capacity as an integer percentage of total capacity
* (with no fractional part).
*/
/** 电池剩余容量,作为总容量的百分比 */
public static final int BATTERY_PROPERTY_CAPACITY = 4;
/**
* Battery remaining energy in nanowatt-hours, as a long integer.
*/
/** 以纳瓦小时考量电池剩余量 */
public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5;
/**
* Battery charge status, from a BATTERY_STATUS_* value.
*/
/** 电池充电状态 */
public static final int BATTERY_PROPERTY_STATUS = 6;
//下面的值表示电池的充电状态
public static final int BATTERY_STATUS_UNKNOWN = Constants.BATTERY_STATUS_UNKNOWN;
public static final int BATTERY_STATUS_CHARGING = Constants.BATTERY_STATUS_CHARGING;
public static final int BATTERY_STATUS_DISCHARGING = Constants.BATTERY_STATUS_DISCHARGING;
public static final int BATTERY_STATUS_NOT_CHARGING = Constants.BATTERY_STATUS_NOT_CHARGING;
public static final int BATTERY_STATUS_FULL = Constants.BATTERY_STATUS_FULL;
}
2、广播方式获取
通过广播获取电池信息
/**
* 获得电池信息
*
* @return
*/
public static BatteryBean getBatteryInfo(Context context) {
BatteryBean batteryBean = new BatteryBean();
try {
Intent batteryStatus = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (batteryStatus != null) {
int temperature = batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int plugState = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
boolean present = batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
String technology = batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
int voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
batteryBean.setStatus(batteryStatus(status));
batteryBean.setTemperature(temperature / 10);
batteryBean.setPlugState(batteryPlugged(plugState));
batteryBean.setHealth(batteryHealth(health));
batteryBean.setPresent(present);
batteryBean.setTechnology(technology);
if (voltage > 1000) {
batteryBean.setVoltage(voltage / 1000f);
} else {
batteryBean.setVoltage(voltage);
}
batteryBean.setPower(getBatteryTotal(context));
batteryBean.setBr(getBatteryCurrent(context));
}
} catch (Exception e) {
}
return batteryBean;
}
private static String batteryHealth(int health) {
String healthBat = Constants.UNKNOWN;
switch (health) {
case BatteryManager.BATTERY_HEALTH_COLD:
healthBat = "cold";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
healthBat = "dead";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthBat = "good";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthBat = "overVoltage";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
healthBat = "overheat";
break;
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
healthBat = "unknown";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthBat = "unspecified";
break;
}
return healthBat;
}
private static String batteryStatus(int status) {
String healthBat = Constants.UNKNOWN;
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING:
healthBat = "charging";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
healthBat = "disCharging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
healthBat = "full";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
healthBat = "notCharging";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
healthBat = "unknown";
break;
}
return healthBat;
}
private static String batteryPlugged(int status) {
String healthBat = Constants.UNKNOWN;
switch (status) {
case BatteryManager.BATTERY_PLUGGED_AC:
healthBat = "ac";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
healthBat = "usb";
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
healthBat = "wireless";
break;
}
return healthBat;
}