天天看點

[Android電量] 耗電資訊統計服務battery / BatteryStats

  • 通過執⾏battery指令(不需要root)
  • adb指令擷取電量量消耗資訊
  • 擷取整個裝置的電量量消耗資訊
  • 擷取某個apk的電量量消耗資訊
  • batterystats使用步驟

通過 adb shell dumpsys battery,傳回結果後有電池的各種資訊,其中就包括

level(百分⽐比)

adb指令檢視電池電量量資訊:

adb shell dumpsys battery

 eroqltechn:/ $ dumpsys battery Current Battery Service state: 
mBootCompleted: true
AC powered: false #有線充電器狀态
USB powered: true #USB連接配接狀态
Wireless powered: false #無線充電狀态
Max charging current: 0 #最大充電電流,機關微安(uA)
Max charging voltage: 0 #最大充電電壓,機關微伏(uV)
Charge counter: 0
status: 2 #充電狀态,UNKNOWN=1,CHARGING=2,DISCHARGING=3,

NOT_CHARGING=4,FULL=5
health: 2 #電池健康狀态:隻有數字2表示goodpresent: true #電池是否安裝在機身
level: 93 #電量: 百分比
scale: 100 #滿電100%
voltage: 4265 #電池電壓
temperature: 289 #電池溫度,機關是0.1攝氏度technology: Li-ion #電池種類batterySWSelfDischarging: falsebatteryMiscEvent: 0
mSecPlugTypeSummary: 2
LED Charging: true
LED Low Battery: true
current now: 217 #電流值,負數表示正在充電charge counter: 0
Adaptive Fast Charging Settings: true

 USE_FAKE_BATTERY: false  SEC_FEATURE_BATTERY_SIMULATION: false  FEATURE_WIRELESS_FAST_CHARGER_CONTROL: true 
 mWasUsedWirelessFastChargerPreviously: false 
 mWirelessFastChargingSettingsEnable: true  BatteryInfoBackUp 
 mSavedBatteryAsoc: 88  mSavedBatteryMaxTemp: 429  mSavedBatteryMaxCurrent: 1746  mSavedBatteryUsage: 6447  FEATURE_SAVE_BATTERY_CYCLE: true           

adb shell dumpsys batterystats | more           

adb shell dumpsys batterystats com.sdu.didi.psnger | more           

由于輸出資訊太多,可使⽤用指令more 或者 less 分篇檢視 輸出資訊如下(由于篇幅, 隻粘貼部分)

heroqltechn:/ $ dumpsys batterystats com.sdu.didi.psnger Discharge step durations:

#0: +1h43m34s304ms to 92 (screen-off, power-save-off, device- idle-on)

#1: +1h54m26s635ms to 93 (screen-off, power-save-off, device- idle-on)

#2: +1h59m33s225ms to 94 (screen-off, power-save-off, device- idle-on)

#3: +1h58m56s325ms to 95 (screen-off, power-save-off, device- idle-on)

#4: +2h2m44s341ms to 96 (screen-off, power-save-off, device-idle- on)

#5: +2h2m20s111ms to 97 (screen-off, power-save-off, device-idle- on)

#6: +1h46m1s361ms to 98 (screen-off, power-save-off, device-idle- on)

 Estimated screen off time: 8d 0h 17m 12s 800ms Estimated screen off device idle time: 8d 0h 17m 12s 800ms 
 Daily stats: Current start time: 2019-01-22-04-46-42 Next min deadline: 2019-01-23-01-00-00 Next max deadline: 2019-01-23-03-00-00 Current daily steps: 
Discharge total time: 7d 19h 32m 18s 800ms (from 3 steps) Discharge screen off time: 7d 19h 32m 18s 800ms (from 3 steps) Discharge screen off device idle time: 7d 19h 32m 18s 800ms

(from 3 steps)
Daily from 2019-01-21-11-43-46 to 2019-01-22-04-46-42:

Discharge total time: 8d 3h 3m 13s 700ms (from 3 steps) Discharge screen off time: 8d 3h 3m 13s 700ms (from 3 steps) Discharge screen off device idle time: 8d 3h 3m 13s 700ms

(from 3 steps)
Charge total time: 6h 35m 21s 800ms (from 18 steps)           

也可以将上述指令标準輸出到⼀一個⽂檔案,來進⾏行行分析。 windows : > xxx.txt Mac/Linux: > xxx.txt

将獲得的資料轉換為可視化的html⽂檔案 指令:python historian.py xxx.txt > xxx.html

關于電量,還可以通過battery-historian⼯工具來擷取。

https://github.com/google/battery-historian

第一步:清除手機電量消耗曆史情況(連接配接手機)

adb shell dumpsys batterystats -enable full-wake-history =====打開全量日志記錄
adb shell dumpsys batterystats --reset =====清空電量資料           

第二步:設計場景測試(注:不連結手機)

\

第三步:導出測試資料

adb shell dumpsys batterystats > d:/batterystat.txt   =======生成TXT檔案(導出到本地)
adb shell dumpsys batterystats > /sdcard/batterystat.txt   =======生成TXT檔案(導出到SD卡)

exit-----退出shell指令