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