Batterystats collects battery data from your device.
Batterystats is part of the Android framework.
Prerequisites:
A mobile device with Developer Options enabled running Android 5.0 or higher.
(前提條件:一臺Android5.0及以上系統(tǒng)的手機,并開啟開發(fā)者選項,打開USB調(diào)試)
1、Download the open-source Battery Historion Python script from GitHub (https://github.com/google/battery-historian).
2、Unzip the file to extract the Battery Historian folder. Inside the folder, find the historian.py file and move it to the Desktop or another writable directory.
3、Connect your mobile device to your computer.
4、On your computer, open a Terminal window.
5、Change to the directory where you've saved historian.py,
for example: cd ~/Desktop
6、Shut down your running adb server.
> adb kill-server
7、Restart adb and check for connected devices.
> adb devices
注意,默認(rèn)情況下wake數(shù)據(jù)不會被采集,為了更好的分析數(shù)據(jù),需要啟用。
> adb shell dumpsys batterystats --enable full-wake-history
8、Reset battery data gathering.
> adb shell dumpsys batterystats --reset
Resetting erases old battery collection data; otherewise, the output will be huge.
9、Disconnect your device from your computer so that you are only drawing current from the device's battery.
拔出USB,非常重要!因為一直插著USB,當(dāng)電充滿,數(shù)據(jù)會被清空。Batterystats只會記錄最后一次充滿電后的記錄。因此建議先把電充滿,完成以上操作后,拔出USB。
10、Play with your app for a short time.
操作要統(tǒng)計電量的應(yīng)用,所有應(yīng)用都會被統(tǒng)計。因此,也可以統(tǒng)計競品的耗電情況。
11、Reconnect your phone.
12、Make sure your phone is recognized: > adb devices
13、Dump all battery data. This can take a while:
> adb shell dumpsys batterystats > batterystats.txt
batterystats.txt里記載了整臺手機的耗電信息
注意,最后要關(guān)閉全量記錄喚醒,因為保持開啟會造成性能問題。
> adb shell dumpsys batterystats --disable full-wake-history
14、Create a HTML version of the data dump for Battery Historian:
> python historian.py batterystats.txt > batterystats.html
15、Open the batterystats.html file in your browser.