• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.resourceschedule.deviceStandby (Device Standby)
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## Modules to Import
9
10```ts
11import deviceStandby from '@ohos.resourceschedule.deviceStandby';
12```
13
14## deviceStandby.getExemptedApps
15
16getExemptedApps(resourceTypes: number, callback: AsyncCallback<Array&lt;ExemptedAppInfo&gt;>): void
17
18Obtains 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.
19
20**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
21
22**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
23
24**System API**: This is a system API.
25
26**Parameters**
27
28| Name     | Type                  | Mandatory  | Description                            |
29| -------- | -------------------- | ---- | ------------------------------ |
30| ResourceTypes|number | Yes   | Resource types. For details, see [ResourceType](#resourcetype).|
31| callback | AsyncCallback<Array&lt;[ExemptedAppInfo](#exemptedappinfo)&gt;> | Yes   |Callback used to return the exempted application information.|
32
33**Error codes**
34
35For details about the error codes, see [backgroundTaskManager Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
36
37| ID | Error Message            |
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**Example**
46
47```ts
48import { BusinessError } from '@ohos.base';
49
50let resourceTypes: deviceStandby.ResourceType  = deviceStandby.ResourceType.TIMER | deviceStandby.ResourceType.NETWORK;
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&lt;ExemptedAppInfo&gt;>
66
67Obtains 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.
68
69**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
70
71**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
72
73**System API**: This is a system API.
74
75**Parameters**
76
77| Name     | Type                  | Mandatory  | Description                            |
78| -------- | -------------------- | ---- | ------------------------------ |
79| ResourceTypes|number | Yes   |Resource types. For details, see [ResourceType](#resourcetype).|
80
81**Return value**
82
83| Type                   | Description                                      |
84| --------------------- | ---------------------------------------- |
85| Promise<Array&lt;[ExemptedAppInfo](#exemptedappinfo)&gt;> | Promise used to return the exempted application information.|
86
87**Error codes**
88
89For details about the error codes, see [backgroundTaskManager Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
90
91| ID | Error Message            |
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**Example**
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
119Requests exemption, so that the application can use restricted resources when the device is in standby mode.
120
121**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
122
123**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
124
125**System API**: This is a system API.
126
127**Parameters**
128
129| Name     | Type                  | Mandatory  | Description                            |
130| -------- | -------------------- | ---- | ------------------------------ |
131| request |[ResourceRequest](#resourcerequest)| Yes   | Request body.|
132
133**Error codes**
134
135For details about the error codes, see [backgroundTaskManager Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
136
137| ID | Error Message            |
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**Example**
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
162Cancels exemption for the application.
163
164**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
165
166**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
167
168**System API**: This is a system API.
169
170**Parameters**
171
172| Name     | Type                  | Mandatory  | Description                            |
173| -------- | -------------------- | ---- | ------------------------------ |
174| request |[ResourceRequest](#resourcerequest)| Yes   | Request body.|
175
176**Error codes**
177
178For details about the error codes, see [backgroundTaskManager Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
179
180| ID | Error Message            |
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**Example**
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
203Enumerates the types of resources that can be used by exempted applications.
204
205**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
206
207**System API**: This is a system API.
208
209|Name  |Value  |Description|
210| ------------ | ------------ |--------------|
211|NETWORK    |1   |Network access resource.|
212|RUNNING_LOCK    |2   |CPU running lock resource.|
213|TIMER     |4   | Timer task resource.|
214|WORK_SCHEDULER     |8   | Work task resource.|
215|AUTO_SYNC      |16   | Automatic synchronization resource.|
216|PUSH     |32   | Push kit resource.|
217|FREEZE       |64   | Freezing application resource.|
218
219## ExemptedAppInfo
220
221Defines the information about an exempted application.
222
223**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
224
225**System API**: This is a system API.
226
227|Name |Type  | Mandatory  |Description  |
228| ------------ | ------------ |------------ | ------------ |
229|resourceTypes   | number  | Yes  |Resource types. For details, see [ResourceType](#resourcetype).  |
230|name   |string   | Yes  |  Name of the application. |
231|duration   | number  | Yes  | Exemption duration.|
232
233## ResourceRequest
234
235Defines the message used to request to be an exempted application.
236
237**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
238
239**System API**: This is a system API.
240
241|Name  |Type  | Mandatory  |Description  |
242| ------------ | ------------ |------------| ------------ |
243|resourceTypes   | number  | Yes  |Resource types. For details, see [ResourceType](#resourcetype).  |
244|uid   | number  | Yes  |UID of the application.  |
245|name   |string   | Yes  | Name of the application. |
246|duration   | number  | Yes  | Exemption duration.|
247|reason   |string   | Yes  |  Reason for the request. |
248