How to improve battery efficiency of background work on Android

Android Developers
16 May 202418:14

Summary

TLDRIn this video, Alice Yuan, a Developer Relations Engineer at Google, and Philip Cuadra from the Android Software Performance team, guide developers on understanding and optimizing app battery efficiency. They discuss the impact of background work on battery drain, the importance of using the right APIs, and best practices for background tasks. They also introduce Perfetto, a performance inspection tool, to analyze power and network usage, demonstrating how to improve battery life by avoiding common pitfalls.

Takeaways

  • 🔋 Background battery drain is a significant issue for users, as seen in Bob's scenario where his device nearly dies despite infrequent use during the day.
  • 🛠️ Developers play a crucial role in improving battery efficiency and preventing uninstalls by ensuring their apps do not contribute to unexpected battery drain.
  • 🔄 Android has introduced several optimizations like Doze, Deep Doze, App Standby buckets, and Foreground Service Usage Restrictions to manage power consumption.
  • 🤔 It's important for developers to select the right APIs for background tasks, considering factors like user value, awareness, and the necessity of immediate feedback.
  • 📚 There are four main categories of APIs for background work: asynchronous APIs with lifecycle-aware components, optimized scheduling APIs, use-case-specific APIs, and Foreground Services.
  • ❓ Developers should ask themselves key questions to determine the appropriate category of API to use, ensuring the app's background tasks are both efficient and user-centric.
  • 🚀 Optimized scheduling APIs like WorkManager and JobScheduler should be used with best practices, such as setting constraints and batching work to minimize device wake-ups.
  • 🚫 Avoid unnecessary use of wakelocks, especially from Foreground Services, as they can lead to inefficient battery usage if not managed correctly.
  • 🔄 Foreground Services should only run for the duration of the user action and be stopped immediately once the action is paused or completed.
  • 📈 Using performance inspection tools like Perfetto can help developers analyze power and network usage, identifying and fixing issues that lead to poor battery efficiency.
  • 🛑 Identifying and addressing common pitfalls such as manually acquiring wakelocks without releasing them, running Foreground Services longer than needed, and making unnecessary network requests can greatly improve battery life.

Q & A

  • What is the main purpose of Alice's presentation?

    -Alice's presentation aims to help developers understand how battery drain happens in the background and how to prevent their apps from causing unexpected battery drain, thereby improving app battery efficiency and preventing users from uninstalling their apps.

  • What are the major contributors to bad-battery user experiences according to Google's internal research?

    -The major contributors to bad-battery user experiences are screen-off battery drain, with cellular Modem, CPU, Wi-Fi, and Bluetooth being the primary hardware components consuming the battery.

  • What role do developers play in managing energy consumption on devices?

    -Developers have a significant influence on how successful a device is at managing energy consumption. They need to select the right APIs for background work and follow best practices to ensure their apps do not contribute to unnecessary battery drain.

  • What are the four categories of APIs available for doing work when an app is no longer visible?

    -The four categories are: 1) using only asynchronous APIs along with lifecycle-aware components; 2) optimized scheduling APIs; 3) use-case-specific APIs; and 4) Foreground Service for scenarios to continue a user action.

  • Why should developers avoid using wakelocks unnecessarily, especially from a Foreground Service?

    -Wakelocks prevent the CPU from sleeping when the screen is off, which can waste battery if managed inefficiently. Developers should ensure that wakelocks are only used when necessary and are released once the task is completed.

  • What are some best practices when using optimized scheduling APIs like WorkManager and JobScheduler?

    -Best practices include declaring constraints to ensure tasks run during optimal scenarios, batching work together when possible, and using the expedited flag only for time-sensitive tasks while being aware of the quotas on runtime for expedited tasks.

  • How can developers ensure that their Foreground Services are used correctly and efficiently?

    -Developers should avoid unnecessary wakelocks, ensure Foreground Services run only for the duration of the user action, and stop the service once the action is paused or completed. They should also ensure network requests are tied to lifecycle-aware components.

  • What is Perfetto and how can it help developers improve battery efficiency?

    -Perfetto is a performance inspection tool that allows developers to manually analyze power and network usage on Android 14+ devices that support On Device Power Monitoring. It helps in identifying and fixing issues related to battery drain and network usage.

  • What are some of the key attributes developers should inspect in a Perfetto trace to optimize for battery drain?

    -Key attributes include screen state, top app, device Suspend or Resume, battery_stats.audio, wakelock usage, network requests, and modem power usage.

  • How can developers leverage Perfetto to detect hard-to-find bugs related to battery efficiency?

    -Developers can record a system trace using Perfetto, inspect the trace for key attributes related to battery and network usage, and identify issues such as unnecessary wakelocks, Foreground Services running longer than needed, and recurring network requests when the app is not visible.

  • What is the significance of the Android system's optimizations like Doze and Deep Doze, App Standby buckets, and Foreground Service Usage Restrictions in managing battery life?

    -These optimizations help in reducing power consumption by managing how and when apps run in the background. They encourage developers to use the right tools and APIs, ensuring that battery life is preserved while still providing a good user experience.

Outlines

plate

このセクションは有料ユーザー限定です。 アクセスするには、アップグレードをお願いします。

今すぐアップグレード

Mindmap

plate

このセクションは有料ユーザー限定です。 アクセスするには、アップグレードをお願いします。

今すぐアップグレード

Keywords

plate

このセクションは有料ユーザー限定です。 アクセスするには、アップグレードをお願いします。

今すぐアップグレード

Highlights

plate

このセクションは有料ユーザー限定です。 アクセスするには、アップグレードをお願いします。

今すぐアップグレード

Transcripts

plate

このセクションは有料ユーザー限定です。 アクセスするには、アップグレードをお願いします。

今すぐアップグレード
Rate This

5.0 / 5 (0 votes)

関連タグ
Battery EfficiencyApp DevelopmentBackground WorkPower ConsumptionOptimized APIsForeground ServicesAndroid SystemPerformance ToolsEnergy ManagementUser ExperienceDevice Optimization
英語で要約が必要ですか?