dumpsys battery
未充电
console:/ # dumpsys battery
Current Battery Service state:AC powered: falseUSB powered: falseWireless powered: falseMax charging current: 0Max charging voltage: 0Charge counter: 1000status: 4health: 2present: truelevel: 90scale: 100voltage: 13temperature: 26technology: Li-ion
console:/ #
充电中
console:/ # dumpsys battery
Current Battery Service state:AC powered: trueUSB powered: falseWireless powered: falseMax charging current: 0Max charging voltage: 0Charge counter: 1000status: 2health: 2present: truelevel: 90scale: 100voltage: 13temperature: 26technology: Li-ion
console:/ #
dumpsys battery -h
console:/ # dumpsys battery -h
Battery service (battery) commands:helpPrint this help text.get [-f] [ac|usb|wireless|status|level|temp|present|counter|invalid]set [-f] [ac|usb|wireless|status|level|temp|present|counter|invalid] <value>Force a battery property value, freezing battery state.-f: force a battery change broadcast be sent, prints new sequence.unplug [-f]Force battery unplugged, freezing battery state.-f: force a battery change broadcast be sent, prints new sequence.reset [-f]Unfreeze battery state, returning to current hardware values.-f: force a battery change broadcast be sent, prints new sequence.suspend_inputSuspend charging even if plugged in.
console:/ #
源码
源码在 frameworks/base/services/core/java/com/android/server/BatteryService.java
,
class Shell extends ShellCommand {@Overridepublic int onCommand(String cmd) {return onShellCommand(this, cmd);}@Overridepublic void onHelp() {PrintWriter pw = getOutPrintWriter();dumpHelp(pw);}}static void dumpHelp(PrintWriter pw) {pw.println("Battery service (battery) commands:");pw.println(" help");pw.println(" Print this help text.");pw.println(" get [-f] [ac|usb|wireless|status|level|temp|present|counter|invalid]");pw.println(" set [-f] [ac|usb|wireless|status|level|temp|present|counter|invalid] <value>");pw.println(" Force a battery property value, freezing battery state.");pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");pw.println(" unplug [-f]");pw.println(" Force battery unplugged, freezing battery state.");pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");pw.println(" reset [-f]");pw.println(" Unfreeze battery state, returning to current hardware values.");pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");if (Build.IS_DEBUGGABLE) {pw.println(" suspend_input");pw.println(" Suspend charging even if plugged in. ");}}static final int OPTION_FORCE_UPDATE = 1<<0;int parseOptions(Shell shell) {String opt;int opts = 0;while ((opt = shell.getNextOption()) != null) {if ("-f".equals(opt)) {opts |= OPTION_FORCE_UPDATE;}}return opts;}int onShellCommand(Shell shell, String cmd) {if (cmd == null) {return shell.handleDefaultCommands(cmd);}PrintWriter pw = shell.getOutPrintWriter();switch (cmd) {case "unplug": {int opts = parseOptions(shell);getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);unplugBattery(/* forceUpdate= */ (opts & OPTION_FORCE_UPDATE) != 0, pw);} break;case "get": {final String key = shell.getNextArg();if (key == null) {pw.println("No property specified");return -1;}switch (key) {case "present":pw.println(mHealthInfo.batteryPresent);break;case "ac":pw.println(mHealthInfo.chargerAcOnline);break;case "usb":pw.println(mHealthInfo.chargerUsbOnline);break;case "wireless":pw.println(mHealthInfo.chargerWirelessOnline);break;case "status":pw.println(mHealthInfo.batteryStatus);break;case "level":pw.println(mHealthInfo.batteryLevel);break;case "counter":pw.println(mHealthInfo.batteryChargeCounterUah);break;case "temp":pw.println(mHealthInfo.batteryTemperatureTenthsCelsius);break;case "invalid":pw.println(mInvalidCharger);break;default:pw.println("Unknown get option: " + key);break;}} break;case "set": {int opts = parseOptions(shell);getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);final String key = shell.getNextArg();if (key == null) {pw.println("No property specified");return -1;}final String value = shell.getNextArg();if (value == null) {pw.println("No value specified");return -1;}try {if (!mUpdatesStopped) {copyV1Battery(mLastHealthInfo, mHealthInfo);}boolean update = true;switch (key) {case "present":mHealthInfo.batteryPresent = Integer.parseInt(value) != 0;break;case "ac":mHealthInfo.chargerAcOnline = Integer.parseInt(value) != 0;break;case "usb":mHealthInfo.chargerUsbOnline = Integer.parseInt(value) != 0;break;case "wireless":mHealthInfo.chargerWirelessOnline = Integer.parseInt(value) != 0;break;case "status":mHealthInfo.batteryStatus = Integer.parseInt(value);break;case "level":mHealthInfo.batteryLevel = Integer.parseInt(value);break;case "counter":mHealthInfo.batteryChargeCounterUah = Integer.parseInt(value);break;case "temp":mHealthInfo.batteryTemperatureTenthsCelsius = Integer.parseInt(value);break;case "invalid":mInvalidCharger = Integer.parseInt(value);break;default:pw.println("Unknown set option: " + key);update = false;break;}if (update) {final long ident = Binder.clearCallingIdentity();try {mUpdatesStopped = true;processValuesLocked(/* forceUpdate= */ (opts & OPTION_FORCE_UPDATE) != 0, pw);} finally {Binder.restoreCallingIdentity(ident);}}} catch (NumberFormatException ex) {pw.println("Bad value: " + value);return -1;}} break;case "reset": {int opts = parseOptions(shell);getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);resetBattery(/* forceUpdate= */ (opts & OPTION_FORCE_UPDATE) != 0, pw);} break;case "suspend_input": {getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);suspendBatteryInput();} break;default:return shell.handleDefaultCommands(cmd);}return 0;}