1. Get the battery capacity of Android device
The battery capacity is mainly obtained by reflecting com.android.internal.os.PowerProfile
Class, calling the getBatteryCapacity method to get it. The specific methods are as follows
/** * Get the capacity of the battery * * @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. Get the current battery usage of Android device
There are two ways to obtain the current usage, one is through the battery service of context.battery service system, the other is through broadcast.
1. System service mode
/** * Get current power percentage * * @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);//Remaining percentage of current power } catch (Exception e) { } return capacity; }
Let's take a look at obtaining the values of other fields in the BatteryManager class of the system
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. */ /** Battery capacity, in microamps */ 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. */ /** Battery current value (microamps), positive value for charging, negative value for discharge */ 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. */ /** Average current (microamps)*/ public static final int BATTERY_PROPERTY_CURRENT_AVERAGE = 3; /** * Remaining battery capacity as an integer percentage of total capacity * (with no fractional part). */ /** Remaining battery capacity as a percentage of total capacity */ public static final int BATTERY_PROPERTY_CAPACITY = 4; /** * Battery remaining energy in nanowatt-hours, as a long integer. */ /** Consider the battery surplus in terms of the nanowatt hour */ public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5; /** * Battery charge status, from a BATTERY_STATUS_* value. */ /** Battery state of charge */ public static final int BATTERY_PROPERTY_STATUS = 6; //The following values indicate the state of charge of the battery 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. Broadcast access
Get battery information through broadcast
/** * Get battery information * * @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; }