harmony 鸿蒙@system.battery (Battery Information)

  • 2025-06-12
  • 浏览 (4)

@system.battery (Battery Information)

The battery module allows you to query the charging status and remaining power of a device.

NOTE - The APIs of this module are no longer maintained since API version 6. It is recommended that you use @ohos.batteryInfo. - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import {Battery, BatteryResponse } from '@kit.BasicServicesKit';

Battery.getStatus(deprecated)

getStatus(options?: GetStatusOptions): void;

Obtains the current charging state and battery level.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

Parameters

Name Type Mandatory Description
options GetStatusOptions No Object that contains the API calling result. This parameter is optional and is left blank by default.

Example

Battery.getStatus({
    success: (data: BatteryResponse) => {
        console.log('success get battery level:' + data.level);
    },
    fail: (data: string, code: number) => {
        console.error('fail to get battery level code:' + code + ', data: ' + data);
    }
});

GetStatusOptions(deprecated)

Object that contains the API calling result.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

Name Type Mandatory Description
success (data: BatteryResponse) => void No Called when an API call is successful. data is a return value of the BatteryResponse type.
fail (data: string, code: number) => void No Called when an API call has failed. data indicates the error information, and code indicates the error code.
complete () => void No Called when an API call is complete.

BatteryResponse(deprecated)

Defines a response that returns the charging status and remaining power of the device.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

Name Type Readable Writable Description
charging boolean Yes No Whether the battery is being charged.
level number Yes No Current battery level, which ranges from 0.00 to 1.00.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

0  赞