Andorid dumpsys battery获取电池信息

news/2024/11/16 13:46:37/

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;}

http://www.ppmy.cn/news/1423084.html

相关文章

容器化Java程序秒级弹性伸缩实践

当前许多数字化转型企业都会将自己的系统微服务化、容器化&#xff0c;这些技术和策略可以帮助企业更有效地利用资源、提高开发运维效率&#xff0c;并且在处理不同流量场景下保证系统的稳定性和性能。 弹性扩缩容 在虚拟机时代&#xff0c;资源的占用和维护成本较高&#xff…

Zynq学习笔记--数字视频帧以及同步信号

目录 1. 介绍 2. 重要概念 3. 仿真测试 4. 总结 1. 介绍 Zynq芯片&#xff0c;作为一款集成了高性能FPGA和ARM处理器的系统级芯片(SoC)&#xff0c;为视频处理提供了强大的硬件支持。该芯片内置的丰富视频方面的IP模块&#xff0c;使得从事视频处理项目的开发者能够高效、…

论文阅读--Conservative Q-Learning for Offline Reinforcement Learning

摘要 在强化学习( RL )中有效地利用以前收集的大量数据集是大规模实际应用的关键挑战。离线RL算法承诺从先前收集的静态数据集中学习有效的策略&#xff0c;而无需进一步的交互。然而&#xff0c;在实际应用中&#xff0c;离线RL是一个主要的挑战&#xff0c;标准的离线RL方法…

详解番茄小说推文如何申请授权,助力你选视频副业收入更上一层楼

小说推文&#xff0c;就是发布小说类视频作品&#xff0c;引导用户到小说平台观看小说。该项目门槛较低&#xff0c;很适合新手&#xff0c;无需高超的技术水平&#xff0c;因此一直备受副业人群关注&#xff0c;尤其是番茄小说平台&#xff0c;因其高佣稳定的优势&#xff0c;…

金蝶云星空与金蝶云星空对接集成委外超耗查询连通生产订单变更(发顺丰)

金蝶云星空与金蝶云星空对接集成委外超耗查询连通生产订单变更(发顺丰) 对接系统金蝶云星空 金蝶K/3Cloud在总结百万家客户管理最佳实践的基础上&#xff0c;提供了标准的管理模式&#xff1b;通过标准的业务架构&#xff1a;多会计准则、多币别、多地点、多组织、多税制应用框…

Unity之OpenXR+XR Interaction Toolkit快速监听手柄任意按键事件

前言 当我们开发一个VR时,有时希望监听一个手柄按键的点击事件,或者一个按钮的Value值等。但是每次有可能监听的按钮有不一样,有可能监听的值不一样,那么每次这么折腾,有点累了,难道就没有一个万能的方法,让我可以直接监听我想要的某个按钮的事件么? 答案是肯定的,今…

Keil MDK下如何设置非零初始化变量 及变量指定位置- Arm CompilerV5 和 V6区别

目录 一、Arm CompilerV51.定义变量时&#xff0c;使用编译器扩展属性__attribute__((section("name"), zero_init))来将变量放入指定段中。其中section("name")选择一个指定的段&#xff0c;zero_init告诉编译器将变量放入ZI段。 二、Arm CompilerV61.定义…

建造者模式:构造复杂对象的艺术

在面向对象的设计中&#xff0c;建造者模式是一种重要的创建型设计模式&#xff0c;专门用来构建复杂的对象。它主要目的是将对象的构造代码与其表示代码分离&#xff0c;使同样的构建过程可以创建不同的表示。本文将详细介绍建造者模式的定义、实现、应用场景以及优缺点&#…