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