openharmony 鸿蒙 js-apis-battery-info-sys

2025-06-12 浏览 (1)

@ohos.batteryInfo (Battery Information) (System API)

The batteryInfo module provides APIs for querying the charger type, battery health status, and battery charging status.

NOTE

The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.batteryInfo (Battery Information).

Modules to Import

import {batteryInfo} from '@kit.BasicServicesKit';

batteryInfo.setBatteryConfig11+

setBatteryConfig(sceneName: string, sceneValue: string): number

Sets the battery configuration based on the specified scenario.

System API: This is a system API.

System capability: SystemCapability.PowerManager.BatteryManager.Core

Parameters

NameTypeMandatoryDescription
sceneNamestringYesScenario name. The value must be a string.
sceneValuestringYesScenario value. The value must be a string.

Return value

TypeDescription
numberOperation result. The value 0 indicates that the operation is successful, and a non-zero value indicates the opposite.

Error codes

For details about the error codes, see Battery Information Error Codes.

IDError Message
4900101Failed to connect to the service.
401Parameter error. Possible causes: 1.Incorrect parameter types.
202Permission verification failed. A non-system application calls a system API.

Example

import {batteryInfo} from '@kit.BasicServicesKit';

let sceneName = 'xxx';
let sceneValue = '0';
let result = batteryInfo.setBatteryConfig(sceneName, sceneValue);

console.info("The result is: " + result);

batteryInfo.getBatteryConfig11+

getBatteryConfig(sceneName: string): string

Obtains the battery configuration based on the specified scenario.

System API: This is a system API.

System capability: SystemCapability.PowerManager.BatteryManager.Core

Parameters

NameTypeMandatoryDescription
sceneNamestringYesScenario name. The value must be a string.

Return value

TypeDescription
stringOperation result. The battery configuration is returned if the operation is successful. Otherwise, "" is returned.

Error codes

For details about the error codes, see Battery Information Error Codes.

IDError Message
4900101Failed to connect to the service.
401Parameter error. Possible causes: 1.Incorrect parameter types.
202Permission verification failed. A non-system application calls a system API.

Example

import {batteryInfo} from '@kit.BasicServicesKit';

let sceneName = 'xxx';
let result = batteryInfo.getBatteryConfig(sceneName);

console.info("The result is: " + result);

batteryInfo.isBatteryConfigSupported11+

isBatteryConfigSupported(sceneName: string): boolean

Checks whether the battery configuration is enabled based on the specified scenario.

System API: This is a system API.

System capability: SystemCapability.PowerManager.BatteryManager.Core

Parameters

NameTypeMandatoryDescription
sceneNamestringYesScenario name. The value must be a string.

Return value

TypeDescription
booleanOperation result. The value true indicates that the charging scenario is supported, and the value false indicates the opposite.

Error codes

For details about the error codes, see Battery Information Error Codes.

IDError Message
4900101Failed to connect to the service.
401Parameter error. Possible causes: 1.Incorrect parameter types.
202Permission verification failed. A non-system application calls a system API.

Example

import {batteryInfo} from '@kit.BasicServicesKit';

let sceneName = 'xxx';
let result = batteryInfo.isBatteryConfigSupported(sceneName);

console.info("The result is: " + result);

Attributes

Describes battery information.

System capability: SystemCapability.PowerManager.BatteryManager.Core

NameTypeReadableWritableDescription
estimatedRemainingChargeTime9+numberYesNoEstimated time for fully charging the current device, in unit of milliseconds. This is a system API.
totalEnergy9+numberYesNoTotal battery capacity of the device, in unit of mAh. This is a system API.
remainingEnergy9+numberYesNoRemaining battery capacity of the device, in unit of mAh. This is a system API.

Example

import {batteryInfo} from '@kit.BasicServicesKit';

let estimatedRemainingChargeTimeInfo: number = batteryInfo.estimatedRemainingChargeTime;
console.info("The estimatedRemainingChargeTimeInfo is: " + estimatedRemainingChargeTimeInfo);

let totalEnergyInfo: number = batteryInfo.totalEnergy;
console.info("The totalEnergyInfo is: " + totalEnergyInfo);

let remainingEnergyInfo: number = batteryInfo.remainingEnergy;
console.info("The remainingEnergyInfo is: " + remainingEnergyInfo);

你可能感兴趣的鸿蒙文章

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

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/XGWlbt