1. 電池信息
1.1 獲取手機電池信息
adb命令:adb shell dumpsys battery
得到信息如下:
1 AC powered: false 2 USB powered: true 3 Wireless powered: false 4 status: 1 #電池狀態:2:充電狀態 ,其他數字為非充電狀態 5 health: 2 #電池健康狀態:只有數字2表示good 6 present: true #電池是否安裝在機身 7 level: 55 #電量: 百分比 8 scale: 100 9 voltage: 3977 #電池電壓 10 current now: -335232 #電流值,負數表示正在充電 11 temperature: 335 #電池溫度,單位是0.1攝氏度 12 technology: Li-poly #電池種類=
1.2 改變手機電池狀態
手機連接到電腦,默認為充電狀態
切換手機電池為非充電狀態: adb shell dumpsys battery set status 1
1.3. 改變手機電量
讓手機電量顯示百分百: adb shell dumpsys battery set level 100
讓手機電量顯示1: adb shell dumpsys battery set level 1
1.4. 軟件角度模擬斷開充電(實際上是充進電)
adb shell dumpsys battery unplug (注意:必須在andorid 6.0以上的版本)
2. 電量消耗信息
關於電量,主要是通過battery-historian工具來獲取。
https://github.com/google/battery-historian
2.1 獲取電量消耗信息
獲取整個設備的電量消耗信息: adb shell dumpsys batterystats | more 獲取某個apk的電量消耗信息: adb shell dumpsys batterystats com.Package.name | more
由於輸出信息太多,可使用命令more 或者 less 分篇查看
輸出信息如下(由於篇幅,只粘貼部分):
1 Battery History: 2 -1d04h22m36s181ms 044 20080000 status=charging health=good plug=usb temp=335 volt=3809 +plugged +sensor 3 -1d04h21m27s713ms 044 640a0000 +wifi +wifi_running +wake_lock 4 -1d04h21m23s278ms 044 6c0a0100 +phone_scanning phone_state=out 5 -1d04h21m19s102ms 044 2c0a0100 -wake_lock 6 -1d04h21m05s005ms 044 6c0a0100 +wake_lock 7 -1d04h20m51s486ms 044 6d0a0100 +wifi_scan 8 -1d04h20m49s211ms 044 6c0a0100 -wifi_scan 9 -1d04h20m41s478ms 044 6c0a0100 10 -1d04h20m31s476ms 044 6d0a0100 +wifi_scan 11 -1d04h20m29s174ms 044 6c0a0100 -wifi_scan 12 -1d04h20m24s353ms 044 2c0a0100 -wake_lock 13 -1d04h20m21s474ms 044 6d0a0100 +wifi_scan +wake_lock 14 -1d04h20m21s125ms 044 6d0a0100 15 -1d04h20m16s847ms 044 2c0a0100 -wifi_scan -wake_lock
也可以將上述命令標准輸出到一個文件,來進行分析。
- windows : > xxx.txt
- Mac/Linux: > xxx.txt
2.2 將獲得的數據轉換為可視化的html文件
命令:python historian.py xxx.txt > xxx.html
Google Python腳本下載地址:https://github.com/google/battery-historian
3. 其他關於android電池電量優秀文章
1.傳輸數據時避免消耗大量電量:
http://hukai.me/android-training-course-in-chinese/connectivity/efficient-downloads/index.html
2.Android性能優化之電量篇:
http://hukai.me/android-performance-battery/