1# @ohos.resourceschedule.deviceStandby (设备待机模块)(系统接口) 2<!--Kit: Background Tasks Kit--> 3<!--Subsystem: ResourceSchedule--> 4<!--Owner: @geng_wd--> 5<!--Designer: @geng_wd--> 6<!--Tester: @wzzqishi--> 7<!--Adviser: @Brilliantry_Rui--> 8 9当设备长时间未被使用或通过按键,可以使设备进入待机模式。待机模式不影响应用使用,还可以延长电池续航时间。通过本模块接口,可查询设备或应用是否为待机模式,以及为应用申请或取消待机资源管控。 10 11> **说明**: 12> 13> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> 本模块接口为系统接口。 16 17## 导入模块 18 19```ts 20import deviceStandby from '@ohos.resourceschedule.deviceStandby'; 21``` 22 23## deviceStandby.getExemptedApps 24 25getExemptedApps(resourceTypes: number, callback: AsyncCallback<Array<ExemptedAppInfo>>): void 26 27获取进入待机模式的应用名单,使用Callback异步回调。 28 29**系统能力:** SystemCapability.ResourceSchedule.DeviceStandby 30 31**需要权限:** ohos.permission.DEVICE_STANDBY_EXEMPTION 32 33 34**参数**: 35 36| 参数名 | 类型 | 必填 | 说明 | 37| -------- | -------------------- | ---- | ------------------------------ | 38| ResourceTypes|number | 是 | 资源类型,类型具体说明请参考[ResourceType](#resourcetype)。 | 39| callback | AsyncCallback<Array<[ExemptedAppInfo](#exemptedappinfo)>> | 是 |豁免应用信息。| 40 41**错误码**: 42 43以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[backgroundTaskManager错误码](errorcode-backgroundTaskMgr.md)。 44 45| 错误码ID | 错误信息 | 46| ---- | --------------------- | 47| 201 | Permission denied. | 48| 202 | Not System App. | 49| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 50| 9800001 | Memory operation failed. | 51| 9800002 | Failed to write data into parcel. Possible reasons: 1. Invalid parameters. | 52| 9800003 | Failed to complete inner transaction. | 53| 9800004 | Failed to get device standby service. Possible cause: A necessary system service is not ready. | 54| 18700001 | Caller information verification failed. | 55 56**示例**: 57 58```ts 59import { BusinessError } from '@ohos.base'; 60 61let resourceTypes: deviceStandby.ResourceType = deviceStandby.ResourceType.TIMER | deviceStandby.ResourceType.NETWORK; 62deviceStandby.getExemptedApps(resourceTypes, (err: BusinessError, res: Array<deviceStandby.ExemptedAppInfo>) => { 63 if (err) { 64 console.error('DEVICE_STANDBY getExemptedApps callback failed. code is: ' + err.code + ',message is: ' + err.message); 65 } else { 66 console.info('DEVICE_STANDBY getExemptedApps callback success.'); 67 for (let i = 0; i < res.length; i++) { 68 console.info('DEVICE_STANDBY getExemptedApps callback result ' + JSON.stringify(res[i])); 69 } 70 } 71}); 72``` 73 74## deviceStandby.getExemptedApps 75 76getExemptedApps(resourceTypes: number): Promise<Array<ExemptedAppInfo>> 77 78获取进入待机模式的应用名单,使用Promise异步回调。 79 80**系统能力:** SystemCapability.ResourceSchedule.DeviceStandby 81 82**需要权限:** ohos.permission.DEVICE_STANDBY_EXEMPTION 83 84 85**参数**: 86 87| 参数名 | 类型 | 必填 | 说明 | 88| -------- | -------------------- | ---- | ------------------------------ | 89| ResourceTypes|number | 是 |资源类型,类型具体说明请参考[ResourceType](#resourcetype)。| 90 91**返回值**: 92 93| 类型 | 说明 | 94| --------------------- | ---------------------------------------- | 95| Promise<Array<[ExemptedAppInfo](#exemptedappinfo)>> | 豁免应用信息。 | 96 97**错误码**: 98 99以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[backgroundTaskManager错误码](errorcode-backgroundTaskMgr.md)。 100 101| 错误码ID | 错误信息 | 102| ---- | --------------------- | 103| 201 | Permission denied. | 104| 202 | Not System App. | 105| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 106| 9800001 | Memory operation failed. | 107| 9800002 | Failed to write data into parcel. Possible reasons: 1. Invalid parameters. | 108| 9800003 | Failed to complete inner transaction. | 109| 9800004 | Failed to get device standby service. Possible cause: A necessary system service is not ready. | 110| 18700001 | Caller information verification failed. | 111 112**示例**: 113 114```ts 115import { BusinessError } from '@ohos.base'; 116 117let resourceTypes: deviceStandby.ResourceType = deviceStandby.ResourceType.TIMER | deviceStandby.ResourceType.NETWORK; 118deviceStandby.getExemptedApps(resourceTypes).then( (res: Array<deviceStandby.ExemptedAppInfo>) => { 119 console.info('DEVICE_STANDBY getExemptedApps promise success.'); 120 for (let i = 0; i < res.length; i++) { 121 console.info('DEVICE_STANDBY getExemptedApps promise result ' + JSON.stringify(res[i])); 122 } 123}).catch( (err: BusinessError) => { 124 console.error('DEVICE_STANDBY getExemptedApps promise failed. code is: ' + err.code + ',message is: ' + err.message); 125}); 126``` 127 128## deviceStandby.requestExemptionResource 129 130requestExemptionResource(request: ResourceRequest): void 131 132应用订阅申请豁免,使应用临时不进入待机管控。 133 134**系统能力:** SystemCapability.ResourceSchedule.DeviceStandby 135 136**需要权限:** ohos.permission.DEVICE_STANDBY_EXEMPTION 137 138 139**参数**: 140 141| 参数名 | 类型 | 必填 | 说明 | 142| -------- | -------------------- | ---- | ------------------------------ | 143| request |[ResourceRequest](#resourcerequest)| 是 | 资源请求。 | 144 145**错误码**: 146 147以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[backgroundTaskManager错误码](errorcode-backgroundTaskMgr.md)。 148 149| 错误码ID | 错误信息 | 150| ---- | --------------------- | 151| 201 | Permission denied. | 152| 202 | Not System App. | 153| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 154| 9800001 | Memory operation failed. | 155| 9800002 | Failed to write data into parcel. Possible reasons: 1. Invalid parameters. | 156| 9800003 | Failed to complete inner transaction. | 157| 9800004 | Failed to get device standby service. Possible cause: A necessary system service is not ready. | 158| 18700001 | Caller information verification failed. | 159 160**示例**: 161 162```ts 163let resRequest: deviceStandby.ResourceRequest = { 164 resourceTypes: deviceStandby.ResourceType.TIMER, 165 uid:10003, 166 name:"com.example.app", 167 duration:10, 168 reason:"apply", 169}; 170deviceStandby.requestExemptionResource(resRequest); 171``` 172 173## deviceStandby.releaseExemptionResource 174 175releaseExemptionResource(request: ResourceRequest): void 176 177取消应用订阅申请豁免。 178 179**系统能力:** SystemCapability.ResourceSchedule.DeviceStandby 180 181**需要权限:** ohos.permission.DEVICE_STANDBY_EXEMPTION 182 183 184**参数**: 185 186| 参数名 | 类型 | 必填 | 说明 | 187| -------- | -------------------- | ---- | ------------------------------ | 188| request |[ResourceRequest](#resourcerequest)| 是 | 资源请求 。| 189 190**错误码**: 191 192以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[backgroundTaskManager错误码](errorcode-backgroundTaskMgr.md)。 193 194| 错误码ID | 错误信息 | 195| ---- | --------------------- | 196| 201 | Permission denied. | 197| 202 | Not System App. | 198| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 199| 9800001 | Memory operation failed. | 200| 9800002 | Failed to write data into parcel. Possible reasons: 1. Invalid parameters. | 201| 9800003 | Failed to complete inner transaction. | 202| 9800004 | Failed to get device standby service. Possible cause: A necessary system service is not ready. | 203| 18700001 | Caller information verification failed. | 204 205**示例**: 206 207```ts 208let resRequest: deviceStandby.ResourceRequest = { 209 resourceTypes: deviceStandby.ResourceType.TIMER, 210 uid:10003, 211 name:"com.demo.app", 212 duration:10, 213 reason:"unapply", 214}; 215deviceStandby.releaseExemptionResource(resRequest); 216``` 217 218## ResourceType 219 220非待机应用资源枚举。 221 222**系统能力:** SystemCapability.ResourceSchedule.DeviceStandby 223 224 225|名称 |值 |说明| 226| ------------ | ------------ |--------------| 227|NETWORK |1 |网络访问资源。| 228|RUNNING_LOCK |2 |cpu-runninglock资源。| 229|TIMER |4 | timer任务资源。| 230|WORK_SCHEDULER |8 | work任务资源。| 231|AUTO_SYNC |16 | 自动同步的资源。 | 232|PUSH |32 | pushkit资源。| 233|FREEZE |64 | 冻结应用资源。| 234 235## ExemptedAppInfo 236 237豁免应用信息,未进入待机管控的应用信息。 238 239**系统能力:** SystemCapability.ResourceSchedule.DeviceStandby 240 241 242|名称 |类型 | 必填 |说明 | 243| ------------ | ------------ |------------ | ------------ | 244|resourceTypes | number | 是 |资源类型,类型具体说明请参考[ResourceType](#resourcetype)。 | 245|name |string | 是 | 应用名。 | 246|duration | number | 是 | 豁免时长。 | 247 248## ResourceRequest 249 250待机资源请求体。 251 252**系统能力:** SystemCapability.ResourceSchedule.DeviceStandby 253 254 255|名称 |类型 | 必填 |说明 | 256| ------------ | ------------ |------------| ------------ | 257|resourceTypes | number | 是 |资源类型,类型具体说明请参考[ResourceType](#resourcetype)。 | 258|uid | number | 是 |应用uid。 | 259|name |string | 是 | 应用名称。 | 260|duration | number | 是 | 豁免时长。 | 261|reason |string | 是 | 申请原因。 |