harmony 鸿蒙@ohos.batteryInfo (Battery Information) (System API)

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

@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

Name Type Mandatory Description
sceneName string Yes Scenario name. The value must be a string.
sceneValue string Yes Scenario value. The value must be a string.

Return value

Type Description
number Operation 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.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Incorrect parameter types.
202 Permission 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

Name Type Mandatory Description
sceneName string Yes Scenario name. The value must be a string.

Return value

Type Description
string Operation 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.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Incorrect parameter types.
202 Permission 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

Name Type Mandatory Description
sceneName string Yes Scenario name. The value must be a string.

Return value

Type Description
boolean Operation 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.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Incorrect parameter types.
202 Permission 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

Name Type Readable Writable Description
estimatedRemainingChargeTime9+ number Yes No Estimated time for fully charging the current device, in unit of milliseconds. This is a system API.
totalEnergy9+ number Yes No Total battery capacity of the device, in unit of mAh. This is a system API.
remainingEnergy9+ number Yes No Remaining 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

0  赞