DEV Community

在人间耕耘
在人间耕耘

Posted on

Battery Monitoring Feature Development Practice in HarmonyOS

Battery Monitoring Feature Development Practice in HarmonyOS

About HarmonyOS 5

HarmonyOS 5 (also known as HarmonyOS Next) represents a revolutionary step in the evolution of Huawei's distributed operating system. As someone who has been following its development closely, I can attest to the remarkable improvements in this version. The system's microkernel architecture not only enhances security but also provides unprecedented flexibility in cross-device collaboration. What excites me most about HarmonyOS 5 is its focus on developer experience - the new ArkTS language, enhanced UI components, and improved debugging tools have made development much more efficient. The distributed capabilities allow us to create truly seamless experiences across different devices, from smartphones to tablets, smart TVs, and IoT devices.

About the Author

I am a dedicated HarmonyOS developer based in China, with over three years of experience in the ecosystem. My journey with HarmonyOS began with its early versions, and I've witnessed its remarkable evolution. As a developer who has contributed to several open-source HarmonyOS projects, I've gained valuable insights into its architecture and best practices. I'm particularly passionate about system tools and performance optimization, which led me to develop this comprehensive battery monitoring module. Through my blog and GitHub repositories, I've been sharing my experiences and helping other developers navigate the exciting world of HarmonyOS development.

Preface

During the development of the HarmonyOS Developer Toolbox, we encountered a practical issue. When debugging performance-sensitive applications, we found that applications exhibited abnormal behavior in low battery mode. This made us realize that, as a developer tool, we needed to add battery monitoring functionality to help developers better debug their applications.

After team discussion, we decided to add battery monitoring functionality to the toolbox. This feature not only displays basic battery information but also helps developers understand how their applications perform under different battery states. After several rounds of development and optimization, we finally implemented a practical battery monitoring module.

I. Feature Overview

In the Developer Toolbox, the battery monitoring feature is placed in the system tools module. This feature originated from our actual development needs. During development, we often need to understand the device's battery status, especially when debugging performance-sensitive applications. Through this feature, developers can:

  • Monitor battery level in real-time
  • Get charging status
  • Monitor battery temperature
  • Get battery health status
  • Low battery alerts

This information is very helpful for developers debugging applications. For example, in low battery mode, the system restricts certain background processes, which may affect application performance. With the battery monitoring feature, developers can quickly identify these issues.

II. Technical Implementation

2.1 Using Battery Management API

Using the @ohos.batteryInfo module provided by HarmonyOS to get battery information. This module doesn't require special permissions and can be used directly:

import batteryInfo from '@ohos.batteryInfo';

// Get battery information
const batteryInfo = batteryInfo.getBatteryInfo();
Enter fullscreen mode Exit fullscreen mode

2.2 Creating Battery Monitoring Service

Created a BatteryMonitor class to manage battery monitoring functionality. Using the singleton pattern to ensure there's only one battery monitoring instance in the entire application:

import batteryInfo from '@ohos.batteryInfo';
import common from '@ohos.app.ability.common';

export class BatteryMonitor {
  // Singleton instance
  private static instance: BatteryMonitor;
  // Application context
  private context: common.UIAbilityContext;
  // Battery information
  private batteryInfo: batteryInfo.BatteryInfo;
  // Battery state change listener
  private batteryListener: batteryInfo.BatteryInfoCallback;

  private constructor(context: common.UIAbilityContext) {
    this.context = context;
    this.initBatteryInfo();
  }

  // Get singleton instance
  public static getInstance(context: common.UIAbilityContext): BatteryMonitor {
    if (!BatteryMonitor.instance) {
      BatteryMonitor.instance = new BatteryMonitor(context);
    }
    return BatteryMonitor.instance;
  }

  // Initialize battery information
  private initBatteryInfo() {
    // Get initial battery information
    this.batteryInfo = batteryInfo.getBatteryInfo();

    // Register battery state change listener
    this.batteryListener = {
      onBatteryInfoChanged: (batteryInfo) => {
        this.handleBatteryInfoChanged(batteryInfo);
      }
    };

    // Start monitoring battery state changes
    batteryInfo.on('batteryInfoChange', this.batteryListener);
  }

  // Handle battery information changes
  private handleBatteryInfoChanged(batteryInfo: batteryInfo.BatteryInfo) {
    const { level, isCharging, temperature } = batteryInfo;

    // Update UI display
    this.updateBatteryUI(level, isCharging, temperature);

    // Low battery alert (when battery level is below 20% and not charging)
    if (level <= 20 && !isCharging) {
      this.showLowBatteryAlert();
    }
  }

  // Update UI display
  private updateBatteryUI(level: number, isCharging: boolean, temperature: number) {
    // Update UI display logic
  }

  // Show low battery alert
  private showLowBatteryAlert() {
    // Show low battery alert
  }

  // Get current battery information
  public getBatteryInfo(): batteryInfo.BatteryInfo {
    return this.batteryInfo;
  }

  // Destroy listener
  public destroy() {
    // Cancel battery state monitoring
    batteryInfo.off('batteryInfoChange', this.batteryListener);
  }
}
Enter fullscreen mode Exit fullscreen mode

2.3 Implementing UI Interface

Designed a clean battery monitoring interface using HarmonyOS's ArkUI framework:

@Entry
@Component
struct BatteryMonitorPage {
  // Battery level
  @State batteryLevel: number = 0;
  // Charging status
  @State isCharging: boolean = false;
  // Battery temperature
  @State temperature: number = 0;
  // Battery monitoring service instance
  private batteryMonitor: BatteryMonitor;

  // Initialize when page appears
  aboutToAppear() {
    this.batteryMonitor = BatteryMonitor.getInstance(getContext(this) as common.UIAbilityContext);
    this.updateBatteryInfo();
  }

  // Update battery information
  updateBatteryInfo() {
    const batteryInfo = this.batteryMonitor.getBatteryInfo();
    this.batteryLevel = batteryInfo.level;
    this.isCharging = batteryInfo.isCharging;
    this.temperature = batteryInfo.temperature;
  }

  build() {
    Column() {
      // Top navigation bar
      Row() {
        Text('Battery Monitor')
          .fontSize(20)
          .fontWeight(FontWeight.Medium)
      }
      .width('100%')
      .height(56)
      .padding({ left: 16, right: 16 })
      .backgroundColor($r('app.color.card_background'))

      // Battery information card
      Column() {
        // Battery icon (charging/not charging state)
        Text(this.isCharging ? '🔌' : '🔋')
          .fontSize(48)
          .margin({ top: 24, bottom: 16 })

        // Battery level display
        Text(`${this.batteryLevel}%`)
          .fontSize(36)
          .fontWeight(FontWeight.Bold)
          .margin({ bottom: 8 })

        // Charging status
        Text(this.isCharging ? 'Charging' : 'Not Charging')
          .fontSize(16)
          .fontColor($r('app.color.text_secondary'))
          .margin({ bottom: 24 })

        // Battery temperature
        Row() {
          Text('Temperature')
            .fontSize(16)
            .fontColor($r('app.color.text_secondary'))
          Text(`${this.temperature}°C`)
            .fontSize(16)
            .fontWeight(FontWeight.Medium)
            .margin({ left: 8 })
        }
        .margin({ bottom: 16 })
      }
      .width('90%')
      .padding(16)
      .backgroundColor($r('app.color.card_background'))
      .borderRadius(16)
      .margin({ top: 16 })
    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.background'))
  }
}
Enter fullscreen mode Exit fullscreen mode

III. Feature Characteristics

The battery monitoring feature has the following characteristics:

3.1 Real-time Monitoring

  • Real-time battery state acquisition
  • Monitor battery level, charging status, temperature, etc.
  • Help developers understand application performance under different battery states

3.2 Low Battery Alerts

  • Automatic alert when battery level is below 20%
  • Customizable alert threshold
  • Avoid issues caused by insufficient battery during development

3.3 UI Display

  • Clear battery state display
  • Dynamic charging status display
  • Real-time temperature information updates
  • Compliant with HarmonyOS design specifications

IV. Development Experience

During development, we summarized some experiences:

4.1 Performance Optimization

  • Use singleton pattern to manage battery monitoring service, reducing memory usage
  • Control update frequency to avoid frequent refreshes (set to update once per second)
  • Pay attention to battery listener lifecycle management to avoid memory leaks

4.2 User Experience

  • Provide intuitive battery state display
  • Add appropriate animation effects (such as charging state transition animation)
  • Support dark mode
  • Keep interface clean

4.3 Error Handling

  • Proper exception handling
  • Provide friendly error prompts
  • Ensure feature stability

V. Problems Encountered

During development, we encountered some issues:

  1. Battery State Update Frequency

    • Problem: Frequent updates causing performance issues
    • Solution: Set 1-second update interval to balance real-time performance and efficiency
  2. Memory Leak Issues

    • Problem: Forgot to release battery state listener
    • Solution: Call destroy method when component is destroyed
  3. Temperature Display Anomalies

    • Problem: Inaccurate temperature display on some devices
    • Solution: Add temperature range check to filter abnormal values

VI. Important Notes

When developing battery monitoring features, pay attention to the following points:

  1. Release battery state listeners promptly to avoid memory leaks
  2. Consider compatibility across different devices
  3. Handle cases of high battery temperature
  4. Ensure UI updates don't affect application performance

VII. Summary

In the Developer Toolbox, the battery monitoring feature provides developers with important system information. Through this feature, developers can:

  • Monitor battery state in real-time
  • Get battery information
  • Design user interfaces
  • Optimize application performance

VIII. Reference Resources

Welcome to Experience

The tool introduced in this article has been integrated into the HarmonyOS Developer Toolbox. Welcome to download and experience more features!

HarmonyOS Developer Toolbox


Author: In the World of Development
Email: 1743914721@qq.com
Copyright Notice: This article is an original work by the CSDN blogger, please include the original source link and this statement when reprinting.

Top comments (0)