直接前往內容

最常造訪的網頁

最近瀏覽的網頁

navigation

Profile Battery Usage with Batterystats and Battery Historian

This walkthrough shows the basic usage and workflow for the Batterystats tool and the Battery Historian script.

Batterystats is a tool included in the Android framework that collects battery data on your device. You can use adb to dump the collected battery data to your development machine and create a report you can analyze using Battery Historian. Battery Historian converts the report from Batterystats into an HTML visualization that you can view in your browser.

What it's good for:

Note: To use Batterystats and Battery Historian, you need a mobile device running Android 5.0 or higher with USB Debugging enabled.

Install Battery Historian

The easiest way to install Battery Historian is to use Docker. For alternate installation methods, including building from source, see the Read Me on the project's GitHub page. To install using Docker, do the following:

  1. Install Docker Community Edition following the instructions on the Docker website.
  2. To confirm that Docker is correctly installed, open the command line and enter the following command:
    docker run hello-world

    If Docker is correctly installed, you will see output like this:

    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    78445dd45222: Pull complete
    Digest:
    sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
  3. Run the Battery Historian image using the following command:
    docker --run -p port_number:9999 gcr.io/android-battery-historian:2.1 --port 9999
    

    Battery Historian will use the port of your choice, as specified using port_number.

    Note: If you are using a Windows machine, you may need to enable virtualization in your BIOS. Check the documentation for your motherboard for more information about how to enable virtualization. If you are able to run the Android Emulator, then virtualization is already enabled.

  4. Navigate to Battery Historian in your browser to confirm that it is running. The address varies depending on your operating system:
    For Linux and Mac
    Battery Historian is available at http://localhost/port_number.
    For Windows
    Once you start Docker, it tells you the IP address of the machine it is using. So, for example, if the IP address is 123.456.78.90, Battery Historian is available at http://123.456.78.90:port_number

    You will see the Battery Historian start page, where you can upload and view battery statistics.

    Figure 1. The start page for Battery Historian

Gather data with Batterystats

To collect data from your device using Batterystats and open it in Battery Historian, do the following:

  1. Connect your mobile device to your computer.
  2. From a Terminal window, shut down your running adb server.
    adb kill-server
    
  3. Restart adb and check for connected devices.
    adb devices
    

    You should see your device listed, similar to the example output below.

    Figure 2. The output of adb devices, showing one connected device

    If you don't see any devices, make sure your phone is connected, and USB Debugging is turned on, and then kill and restart adb.

  4. Reset battery data gathering.
    adb shell dumpsys batterystats --reset
    

    The device is always collecting batterystats and other debugging information in the background. Resetting erases old battery collection data. If you do not reset, the output will be huge.

  5. Disconnect your device from your computer so that you are only drawing current from the device's battery.
  6. Play with your app and perform actions for which you would like data; for example, disconnect from WiFi and send data to the cloud.
  7. Reconnect your phone.
  8. Make sure your phone is recognized:
    adb devices
    
  9. Dump all battery data. This can take a while:
    adb shell dumpsys batterystats > [path/]batterystats.txt
    

    The batterystats.txt file is created in the directory you specify using the optional path argument. If you do not specify a path, the file is created in your home directory.

  10. Create a report from raw data.
    For devices running Android 7.0 and higher:
    adb bugreport > [path/]bugreport.zip
    
    For devices running Android 6.0 and lower:
    adb bugreport > [path/]bugreport.txt
    

    Bugreport may take several minutes to complete. Do not disconnect your device or cancel the process until it is complete.

    As with batterystats.txt above, these files are created in the directory you specify using the optional path argument. If you do not specify a path, they are created in your home directory.

  11. If it's not already running, run Battery Historian using the following command:
    docker --run -p port_number:9999 gcr.io/android-battery-historian:2.1 --port 9999
    
  12. To view your data in Battery Historian, open the Battery Historian in your browser. (For Mac and Linux, Battery Historian runs at http://localhost/port_number. For Windows, Battery Historian runs at http://your_IP_address/port_number.)
  13. Click Browse and then choose the bugreport file you created above.
  14. Click Submit. Battery Historian will open a chart created from your batterystats data.

View data with Battery Historian charts

The Battery Historian chart graphs power-relevant events over time.

Each row shows a colored bar segment when a system component is active and thus drawing current from the battery. The chart does not show how much battery was used by the component, only that the app was active. Charts are organized by category, showing a bar for each category over time, as displayed on the X-axis of the chart.

Figure 3. Example of a Battery Historian chart

  1. Add additional metrics from the dropdown list.
  2. Hover over the information icons to see more information about each metric, including a key for the colors used in the chart.
  3. Hover over a bar to see more detailed information about that metric and the battery stats at a specific point on the timeline.

Additional batterystats output

You can view additional information from the batterystats.txt file in the stats section below the Battery Historian chart.

Figure 4. The stats section of Battery Historian

The 1 System Stats tab includes system-wide statistics, such as cell signal levels and screen brightness. This information provides an overall picture of what's happening with the device. This is especially useful to make sure no external events are affecting your test.

The 2 App Stats tab includes information about specific apps. Sort the list of apps using the 3 Sort apps by dropdown list in the App Selection pane on the left. You can select a specific app to view stats for using the 4 apps dropdown list below.

For more information about battery performance, see Optimizing Battery Life.

本網站使用 Cookie 儲存你在此指定的語言和顯示選項偏好設定。

掌握有關 Android 開發人員的最新消息和實用訣竅,協助你製作最受歡迎的 Google Play 內容。

* 必填欄位

訂閱成功!

在 WeChat 上追蹤 Google Developers

你要以瀏覽這個網站嗎?

你要求以顯示這個網頁,但你為此網站指定的語言偏好設定為

是否要變更語言偏好設定並改用瀏覽網站?稍後如要變更語言偏好設定,請利用位在每個網頁最下方的語言選單來調整設定。

你的 API 層級必須達 以上才能存取這個級別

本說明文件已隱藏,因為你為該文件選取的 API 層級為 。使用左側導覽列上方的選取工具即可變更說明文件的 API 層級。

如需進一步瞭解如何為應用程式指定 API 層級的相關資訊,請參閱 Supporting Different Platform Versions (支援不同的平台版本) (英文)。

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)