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