一.通过广播获取到的信息
IntentFilter filter2 = new IntentFilter();
filter2.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(filter2,receiver);
接收到广播后,用getIntExtra("level")和getIntExtra("scale")获得相应值
intent.getIntExtra("level", 0); //电池剩余电量
intent.getIntExtra("scale", 0); //获取电池满电量数值
intent.getStringExtra("technology"); //获取电池技术支持
intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN); //获取电池状态
intent.getIntExtra("plugged", 0); //获取充电方式
intent.getIntExtra("health",BatteryManager.BATTERY_HEALTH_UNKNOWN); //获取电池健康度
intent.getIntExtra("voltage", 0); //获取电池电压,单位为mV
intent.getIntExtra("temperature", 0); //获取电池温度,获取到的值除以10以后得到℃
- level和scale获取的不是电量的绝对值,只能使用levle/scale获取剩余电量的百分比
- 获取电池健康状态
val health =
intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN) ///获取电池健康度
when (health) {
BatteryManager.BATTERY_HEALTH_GOOD -> {
tv_battery_health.text = "良好"
}
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> {
tv_battery_health.text = "电压过高"
}
BatteryManager.BATTERY_HEALTH_COLD -> {
tv_battery_health.text = "过冷"
}
BatteryManager.BATTERY_HEALTH_DEAD -> {
tv_battery_health.text = "损坏"
}
BatteryManager.BATTERY_HEALTH_OVERHEAT -> {
tv_battery_health.text = "过热"
}
else -> {
tv_battery_health.text = "未知"
}
}
3.获取电池充电状态
intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN); //获取电池状态
获取到的值有以下情况
public static final int BATTERY_STATUS_UNKNOWN
public static final int BATTERY_STATUS_CHARGING //充电中
public static final int BATTERY_STATUS_DISCHARGING //放电中
public static final int BATTERY_STATUS_NOT_CHARGING //未充电
public static final int BATTERY_STATUS_FULL //已充满
- 获取充电方式
intent.getIntExtra("plugged", 0); //获取充电方式
int BATTERY_PLUGGED_AC 使用充电器充电
int BATTERY_PLUGGED_USB 使用USB充电
int BATTERY_PLUGGED_WIRELESS 使用无线方式充电
二.通过dumpsys battery获取到的信息
adb shell dumpsys battery
AC powered: false ///有线充电器状态
USB powered: true ///USB连接状态
Wireless powered: false ///无线充电状态
Max charging current: 500000 ///最大充电电流,单位微安(uA)
Max charging voltage: 5000000 ///最大充电电压,单位微伏(uV)
Charge counter: 4149000 ///
status: 2 ///充电状态,UNKNOWN=1,CHARGING=2,DISCHARGING=3,NOT_CHARGING=4,FULL=5
health: 2 ///电池健康状态
present: true ///
level: 95 ///当前95%
scale: 100 ///满电100%
voltage: 4244 ///电压
temperature: 250 ///温度
technology: Li-ion
三.BatteryManager获取到的信息
经测试: BATTERY_PROPERTY_CURRENT_NOW和BATTERY_PROPERTY_CURRENT_AVERAGE获取到的数据并不准确
测量设备电耗
API >= 21 (5.0,L)
BatteryManager manager = (BatteryManager) getSystemService(BATTERY_SERVICE);
manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);//剩余电量,单位 (mAH)微安小时
manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);//当前电流,单位mA
manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);//当前电流,单位mA
manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);///当前电量百分比
API >= 26 (8.0,O)
manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);///充电状态
获取电池的总容量
public static double getBatteryCapacity(Context context) {
Object mPowerProfile;
double batteryCapacity = 0;
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();
}
String info = batteryCapacity + " mAh";
Log.i(TAG,"电池容量是: "+batteryCapacity);
return batteryCapacity;
}
知道电池总容量后可以根据当前电量的百分比,计算出当前电量。