1# @ohos.resourceschedule.deviceStandby (Device Standby) (System API)
2A device enters standby mode if it is unused for a long period of time or after the Power button is pressed. The standby mode prolongs the battery life without affecting the use of applications. The **deviceStandby** module provides APIs for you to check whether a device is in standby mode and request or cancel standby resource control for an application.
3
4>  **NOTE**
5>
6> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7>
8> The APIs provided by this module are system APIs.
9
10## Modules to Import
11
12```ts
13import deviceStandby from '@ohos.resourceschedule.deviceStandby';
14```
15
16## deviceStandby.getExemptedApps
17
18getExemptedApps(resourceTypes: number, callback: AsyncCallback<Array&lt;ExemptedAppInfo&gt;>): void
19
20Obtains the list of applications that can still use resources of the specified types when the device is in standby mode. This API uses an asynchronous callback to return the result.
21
22**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
23
24**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
25
26
27**Parameters**
28
29| Name     | Type                  | Mandatory  | Description                            |
30| -------- | -------------------- | ---- | ------------------------------ |
31| ResourceTypes|number | Yes   | Resource types. For details, see [ResourceType](#resourcetype).|
32| callback | AsyncCallback<Array&lt;[ExemptedAppInfo](#exemptedappinfo)&gt;> | Yes   |Callback used to return the exempted application information.|
33
34**Error codes**
35
36For details about the error codes, see [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.md) and [Universal Error Codes](../errorcode-universal.md).
37
38| ID | Error Message            |
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**Example**
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&lt;ExemptedAppInfo&gt;>
70
71Obtains the list of applications that can still use resources of the specified types when the device is in standby mode. This API uses a promise to return the result.
72
73**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
74
75**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
76
77
78**Parameters**
79
80| Name     | Type                  | Mandatory  | Description                            |
81| -------- | -------------------- | ---- | ------------------------------ |
82| ResourceTypes|number | Yes   |Resource types. For details, see [ResourceType](#resourcetype).|
83
84**Return value**
85
86| Type                   | Description                                      |
87| --------------------- | ---------------------------------------- |
88| Promise<Array&lt;[ExemptedAppInfo](#exemptedappinfo)&gt;> | Promise used to return the exempted application information.|
89
90**Error codes**
91
92For details about the error codes, see [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.md) and [Universal Error Codes](../errorcode-universal.md).
93
94| ID | Error Message            |
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**Example**
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
125Requests exemption, so that the application can use restricted resources when the device is in standby mode.
126
127**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
128
129**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
130
131
132**Parameters**
133
134| Name     | Type                  | Mandatory  | Description                            |
135| -------- | -------------------- | ---- | ------------------------------ |
136| request |[ResourceRequest](#resourcerequest)| Yes   | Request body.|
137
138**Error codes**
139
140For details about the error codes, see [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.md) and [Universal Error Codes](../errorcode-universal.md).
141
142| ID | Error Message            |
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**Example**
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
170Cancels exemption for the application.
171
172**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
173
174**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
175
176
177**Parameters**
178
179| Name     | Type                  | Mandatory  | Description                            |
180| -------- | -------------------- | ---- | ------------------------------ |
181| request |[ResourceRequest](#resourcerequest)| Yes   | Request body.|
182
183**Error codes**
184
185For details about the error codes, see [backgroundTaskManager Error Codes](errorcode-backgroundTaskMgr.md) and [Universal Error Codes](../errorcode-universal.md).
186
187| ID | Error Message            |
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**Example**
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
213Enumerates the types of resources that can be used by exempted applications.
214
215**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
216
217
218|Name  |Value  |Description|
219| ------------ | ------------ |--------------|
220|NETWORK    |1   |Network access resource.|
221|RUNNING_LOCK    |2   |CPU running lock resource.|
222|TIMER     |4   | Timer task resource.|
223|WORK_SCHEDULER     |8   | Work task resource.|
224|AUTO_SYNC      |16   | Automatic synchronization resource.|
225|PUSH     |32   | Push kit resource.|
226|FREEZE       |64   | Freezing application resource.|
227
228## ExemptedAppInfo
229
230Defines the information about an exempted application.
231
232**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
233
234
235|Name |Type  | Mandatory  |Description  |
236| ------------ | ------------ |------------ | ------------ |
237|resourceTypes   | number  | Yes  |Resource types. For details, see [ResourceType](#resourcetype).  |
238|name   |string   | Yes  |  Name of the application. |
239|duration   | number  | Yes  | Exemption duration.|
240
241## ResourceRequest
242
243Defines the message used to request to be an exempted application.
244
245**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
246
247
248|Name  |Type  | Mandatory  |Description  |
249| ------------ | ------------ |------------| ------------ |
250|resourceTypes   | number  | Yes  |Resource types. For details, see [ResourceType](#resourcetype).  |
251|uid   | number  | Yes  |UID of the application.  |
252|name   |string   | Yes  | Name of the application. |
253|duration   | number  | Yes  | Exemption duration.|
254|reason   |string   | Yes  |  Reason for the request. |
255