• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.batteryInfo (电量信息)(系统接口)
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: PowerManager-->
5<!--Owner: @zhang-yinglie; @volcano_wang-->
6<!--Designer: @wangyantian0-->
7<!--Tester: @alien0208-->
8<!--Adviser: @w_Machine_cc-->
9
10该模块主要提供电池状态和充放电状态的查询接口。
11
12> **说明:**
13>
14> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15>
16>当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.batteryInfo (电量信息)](js-apis-battery-info.md)。
17
18
19## 导入模块
20
21```js
22import {batteryInfo} from '@kit.BasicServicesKit';
23```
24
25## batteryInfo.setBatteryConfig<sup>11+</sup>
26
27setBatteryConfig(sceneName: string, sceneValue: string): number
28
29按场景名称设置电池配置。
30
31**系统接口:** 此接口为系统接口。
32
33**系统能力:** SystemCapability.PowerManager.BatteryManager.Core
34
35**参数**:
36
37| 参数名     | 类型   | 必填 | 说明         |
38| ---------- | ------ | ---- | ------------ |
39| sceneName  | string | 是   | 设置场景名称;该参数必须为字符串类型。 |
40| sceneValue | string | 是   | 设置场景的值;该参数必须为字符串类型。 |
41
42**返回值**:
43
44| 类型   | 说明                                                       |
45| ------ | ---------------------------------------------------------- |
46| number | 返回设置充电结果。返回0表示设置成功,返回非0表示设置失败。 |
47
48**错误码:**
49
50以下错误码的详细介绍请参见[电量信息错误码](errorcode-battery-info.md)和[通用错误码](../errorcode-universal.md)。
51
52| 错误码ID   | 错误信息    |
53|---------|---------|
54| 5100101 | Failed to connect to the service. |
55| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
56| 202     | Permission verification failed. A non-system application calls a system API. |
57
58**示例**:
59
60  ```ts
61  import {batteryInfo} from '@kit.BasicServicesKit';
62
63  let sceneName = 'xxx';
64  let sceneValue = '0';
65  let result = batteryInfo.setBatteryConfig(sceneName, sceneValue);
66
67  console.info("The result is: " + result);
68  ```
69
70## batteryInfo.getBatteryConfig<sup>11+</sup>
71
72getBatteryConfig(sceneName: string): string
73
74按场景名称查询电池配置。
75
76**系统接口:** 此接口为系统接口。
77
78**系统能力:** SystemCapability.PowerManager.BatteryManager.Core
79
80**参数**:
81
82| 参数名    | 类型   | 必填 | 说明         |
83| --------- | ------ | ---- | ------------ |
84| sceneName | string | 是   | 设置场景名称;该参数必须为字符串类型。 |
85
86**返回值**:
87
88| 类型   | 说明                           |
89| ------ | ------------------------------ |
90| string | 返回电池充电配置,否则返回""。 |
91
92**错误码:**
93
94以下错误码的详细介绍请参见[电量信息错误码](errorcode-battery-info.md)和[通用错误码](../errorcode-universal.md)。
95
96| 错误码ID   | 错误信息    |
97|---------|---------|
98| 5100101 | Failed to connect to the service. |
99| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
100| 202     | Permission verification failed. A non-system application calls a system API. |
101
102**示例**:
103
104  ```ts
105  import {batteryInfo} from '@kit.BasicServicesKit';
106
107  let sceneName = 'xxx';
108  let result = batteryInfo.getBatteryConfig(sceneName);
109
110  console.info("The result is: " + result);
111  ```
112
113## batteryInfo.isBatteryConfigSupported<sup>11+</sup>
114
115isBatteryConfigSupported(sceneName: string): boolean
116
117检查是否按场景名称启用电池配置。
118
119**系统接口:** 此接口为系统接口。
120
121**系统能力:** SystemCapability.PowerManager.BatteryManager.Core
122
123**参数**:
124
125| 参数名    | 类型   | 必填 | 说明         |
126| --------- | ------ | ---- | ------------ |
127| sceneName | string | 是   | 设置场景名称;该参数必须为字符串类型。 |
128
129**返回值**:
130
131| 类型    | 说明                                              |
132| ------- | ------------------------------------------------- |
133| boolean | 如果设备支持充电场景,则返回true,否则返回false。 |
134
135**错误码:**
136
137以下错误码的详细介绍请参见[电量信息错误码](errorcode-battery-info.md)和[通用错误码](../errorcode-universal.md)。
138
139| 错误码ID   | 错误信息    |
140|---------|---------|
141| 5100101 | Failed to connect to the service. |
142| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
143| 202     | Permission verification failed. A non-system application calls a system API. |
144
145**示例**:
146
147  ```ts
148  import {batteryInfo} from '@kit.BasicServicesKit';
149
150  let sceneName = 'xxx';
151  let result = batteryInfo.isBatteryConfigSupported(sceneName);
152
153  console.info("The result is: " + result);
154  ```
155
156## 属性
157
158描述电池信息。
159
160**系统能力**:SystemCapability.PowerManager.BatteryManager.Core
161
162| 名称      | 类型        | 只读 | 可选 |  说明     |
163| --------------- | ------------------- | ---- | ---- | ---------------------|
164| estimatedRemainingChargeTime<sup>9+</sup> | number                                         | 是   | 否   | 表示当前设备充满电的预估时间,单位毫秒。此接口为系统接口。          |
165| totalEnergy<sup>9+</sup>                  | number                                         | 是   | 否   | 表示当前设备电池的总容量,单位毫安时。此接口为系统接口。   |
166| remainingEnergy<sup>9+</sup>              | number                                         | 是   | 否   | 表示当前设备电池的剩余容量,单位毫安时。此接口为系统接口。 |
167
168**示例**:
169  ```ts
170  import {batteryInfo} from '@kit.BasicServicesKit';
171
172  let estimatedRemainingChargeTimeInfo: number = batteryInfo.estimatedRemainingChargeTime;
173  console.info("The estimatedRemainingChargeTimeInfo is: " + estimatedRemainingChargeTimeInfo);
174
175  let totalEnergyInfo: number = batteryInfo.totalEnergy;
176  console.info("The totalEnergyInfo is: " + totalEnergyInfo);
177
178  let remainingEnergyInfo: number = batteryInfo.remainingEnergy;
179  console.info("The remainingEnergyInfo is: " + remainingEnergyInfo);
180  ```
181
182