js-apis-system-battery.md 2.4 KB
Newer Older
1
# @system.battery (Battery Information)
Z
zengyawen 已提交
2

3 4 5 6 7
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`](js-apis-battery-info.md) 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.
Z
zengyawen 已提交
8 9 10 11 12


## Modules to Import


S
shawn_he 已提交
13
```js
Z
zengyawen 已提交
14 15 16 17 18 19
import battery from '@system.battery';
```


## battery.getStatus

20
getStatus(options?: GetStatusOptions): void;
Z
zengyawen 已提交
21 22 23 24 25

Obtains the current charging state and battery level.

**System capability**: SystemCapability.PowerManager.BatteryManager.Core

26
**Parameters**
Z
zengyawen 已提交
27

28
| Name| Type| Mandatory| Description|
Z
zengyawen 已提交
29
| -------- | -------- | -------- | -------- |
30
| options | [GetStatusOptions](#getstatusoptions) | No| Object that contains the API calling result.|
Z
zengyawen 已提交
31 32 33

**Example**

S
shawn_he 已提交
34
```js
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
battery.getStatus({
    success: function(data) {
        console.log('success get battery level:' + data.level);
    },
    fail: function(data, code) {
        console.error('fail to get battery level code:' + code + ', data: ' + data);
    }
});
```

## GetStatusOptions

Object that contains the API calling result.

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

## BatteryResponse

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

| Name| Type| Description|
| -------- | -------- | -------- |
| charging | boolean | Whether the battery is being charged.|
| level | number | Current battery level, which ranges from **0.00** to **1.00**.|