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