js-apis-system-battery.md 1.8 KB
Newer Older
Z
zengyawen 已提交
1 2
# Battery Level

S
shawn_he 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
4 5 6 7 8 9 10 11
> - The APIs of this module are no longer maintained since API version 7. 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.


## Modules to Import


S
shawn_he 已提交
12
```js
Z
zengyawen 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import battery from '@system.battery';
```


## battery.getStatus

getStatus(Object): void

Obtains the current charging state and battery level.

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

**Parameter**

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| success | Function | No | Called when the check result is obtained |
| fail | Function | No | Called when the check result fails to be obtained |
| complete | Function | No | Called when the execution is complete |

The following value will be returned when the check result is obtained.

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

**Example**

S
shawn_he 已提交
42
```js
Z
zengyawen 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
export default {    
  getStatus() {       
    battery.getStatus({           
      success: function(data) {               
        console.log('success get battery level:' + data.level);           
      },            
      fail: function(data, code) {                
        console.log('fail to get battery level code:' + code + ', data: ' + data);            
      },        
    });    
  },
}
```