harmony 鸿蒙@system.battery (Battery Information)

2022-08-09 浏览 (871)

@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 instead.
  • 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 '@system.battery';

battery.getStatus

getStatus(options?: GetStatusOptions): void;

Obtains the current charging state and battery level.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

Parameters

NameTypeMandatoryDescription
optionsGetStatusOptionsNoObject 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

Object that contains the API calling result.

System capability: SystemCapability.PowerManager.BatteryManager.Lite

NameTypeMandatoryDescription
success(data: BatteryResponse) => voidNoCalled when an API call is successful. data is a return value of the BatteryResponse type.
fail(data: string, code: number) => voidNoCalled when an API call has failed. data indicates the error information, and code indicates the error code.
complete() => voidNoCalled when an API call is complete.

BatteryResponse

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

System capability: SystemCapability.PowerManager.BatteryManager.Lite

NameTypeReadableWritableDescription
chargingbooleanYesNoWhether the battery is being charged.
levelnumberYesNoCurrent battery level, which ranges from 0.00 to 1.00.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

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