提交 692c51d9 编写于 作者: G Gloria

Update docs against 20328

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 67d5f551
# @ohos.resourceschedule.deviceStandby (Device Standby)
A 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.
> **NOTE**
>
> 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.
## Modules to Import
```js
import deviceStandby from '@ohos.resourceschedule.deviceStandby';
```
## deviceStandby.isDeviceInStandby
isDeviceInStandby(callback: AsyncCallback&lt;boolean&gt;): void;
Checks whether the device is in standby mode. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | -------------------- | ---- | ------------------------------ |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback used to return whether the device is standby mode.|
**Error codes**
For details about the error codes, see [Background Task Management Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
| ID | Error Message |
| ---- | --------------------- |
| 9800001 | Memory operation failed. |
| 9800002 | Parcel operation failed. |
| 9800003 | Inner transact failed. |
| 9800004 | System service operation failed. |
| 18700001 | Caller information verification failed. |
**Example**
```js
try{
deviceStandby.isDeviceInStandby((err, res) => {
if (err) {
console.log('DEVICE_STANDBY isDeviceInStandby callback failed. code is: ' + err.code + ',message is: ' + err.message);
} else {
console.log('DEVICE_STANDBY isDeviceInStandby callback succeeded, result: ' + JSON.stringify(res));
}
});
} catch(error) {
console.log('DEVICE_STANDBY isDeviceInStandby throw error, code is: ' + error.code + ',message is: ' + error.message);
}
```
## deviceStandby.isDeviceInStandby
isDeviceInStandby(): Promise&lt;boolean&gt;
Checks whether the device is in standby mode. This API uses a promise to return the result.
**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**Return value**
| Type | Description |
| --------------------- | ---------------------------------------- |
| Promise&lt;boolean&gt; | Promise used to return whether the device is in standby mode.|
**Error codes**
For details about the error codes, see [Background Task Management Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
| ID | Error Message |
| ---- | --------------------- |
| 9800001 | Memory operation failed. |
| 9800002 | Parcel operation failed. |
| 9800003 | Inner transact failed. |
| 9800004 | System service operation failed. |
| 18700001 | Caller information verification failed. |
**Example**
```js
try{
deviceStandby.isDeviceInStandby().then( res => {
console.log('DEVICE_STANDBY isDeviceInStandby promise succeeded, result: ' + JSON.stringify(res));
}).catch( err => {
console.log('DEVICE_STANDBY isDeviceInStandby promise failed. code is: ' + err.code + ',message is: ' + err.message);
});
} catch (error) {
console.log('DEVICE_STANDBY isDeviceInStandby throw error, code is: ' + error.code + ',message is: ' + error.message);
}
```
## deviceStandby.getExemptedApps
getExemptedApps(resourceTypes: number, callback: AsyncCallback<Array&lt;ExemptedAppInfo&gt;>): void;
Obtains 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.
**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | -------------------- | ---- | ------------------------------ |
| [ResourceTypes](#resourcetype)|number | Yes | Types of resources that can be used.|
| callback | AsyncCallback<Array&lt;[ExemptedAppInfo](#exemptedappinfo)&gt;> | Yes |Callback used to return the exempted application information.|
**Error codes**
For details about the error codes, see [Background Task Management Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
| ID | Error Message |
| ---- | --------------------- |
| 9800001 | Memory operation failed. |
| 9800002 | Parcel operation failed. |
| 9800003 | Inner transact failed. |
| 9800004 | System service operation failed. |
| 18700001 | Caller information verification failed. |
**Example**
```js
try{
deviceStandby.getExemptedApps(resourceTypes, (err, res) => {
if (err) {
console.log('DEVICE_STANDBY getExemptedApps callback failed. code is: ' + err.code + ',message is: ' + err.message);
} else {
console.log('DEVICE_STANDBY getExemptedApps callback success.');
for (let i = 0; i < res.length; i++) {
console.log('DEVICE_STANDBY getExemptedApps callback result ' + JSON.stringify(res[i]));
}
}
});
} catch (error) {
console.log('DEVICE_STANDBY getExemptedApps throw error, code is: ' + error.code + ',message is: ' + error.message);
}
```
## deviceStandby.getExemptedApps
getExemptedApps(resourceTypes: number): Promise<Array&lt;ExemptedAppInfo&gt;>;
Obtains the list of applications that can still use resources of the specified type when the device is in standby mode. This API uses a promise to return the result.
**System capability**: SystemCapability.ResourceSchedule.DeviceStandby
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | -------------------- | ---- | ------------------------------ |
| [ResourceTypes](#resourcetype)|number | Yes |Types of resources that can be used.|
**Return value**
| Type | Description |
| --------------------- | ---------------------------------------- |
| Promise<Array&lt;[ExemptedAppInfo](#exemptedappinfo)&gt;> | Promise used to return the exempted application information.|
**Error codes**
For details about the error codes, see [Background Task Management Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
| ID | Error Message |
| ---- | --------------------- |
| 201 | Permission denied. |
| 202 | Not System App. |
| 401 | Parameter error. |
| 9800001 | Memory operation failed. |
| 9800002 | Parcel operation failed. |
| 9800003 | Inner transact failed. |
| 9800004 | System service operation failed. |
| 18700001 | Caller information verification failed. |
**Example**
```js
try{
deviceStandby.getExemptedApps(resourceTypes).then( res => {
console.log('DEVICE_STANDBY getExemptedApps promise success.');
for (let i = 0; i < res.length; i++) {
console.log('DEVICE_STANDBY getExemptedApps promise result ' + JSON.stringify(res[i]));
}
}).catch( err => {
console.log('DEVICE_STANDBY getExemptedApps promise failed. code is: ' + err.code + ',message is: ' + err.message);
});
} catch (error) {
console.log('DEVICE_STANDBY getExemptedApps throw error, code is: ' + error.code + ',message is: ' + error.message);
}
```
## deviceStandby.requestExemptionResource
requestExemptionResource(request: ResourceRequest): void;
Requests exemption, so that the application can use restricted resources when the device is in standby mode.
**System capability**: SystemCapability.ResourceSchedule.DeviceStandby.Exemption
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | -------------------- | ---- | ------------------------------ |
| request |[ResourceRequest](#resourcerequest)| Yes | Request body.|
**Error codes**
For details about the error codes, see [Background Task Management Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
| ID | Error Message |
| ---- | --------------------- |
| 9800001 | Memory operation failed. |
| 9800002 | Parcel operation failed. |
| 9800003 | Inner transact failed. |
| 9800004 | System service operation failed. |
| 18700001 | Caller information verification failed. |
**Example**
```js
let resRequest = {
resourceTypes: 1,
uid:10003,
name:"com.example.app",
duration:10,
reason:"apply",
};
// Promise mode
try{
deviceStandby.requestExemptionResource(resRequest).then( () => {
console.log('DEVICE_STANDBY requestExemptionResource promise succeeded.');
}).catch( err => {
console.log('DEVICE_STANDBY requestExemptionResource promise failed. code is: ' + err.code + ',message is: ' + err.message);
});
} catch (error) {
console.log('DEVICE_STANDBY requestExemptionResource throw error, code is: ' + error.code + ',message is: ' + error.message);
}
// Asynchronous callback mode
try{
deviceStandby.requestExemptionResource(resRequest, (err) => {
if (err) {
console.log('DEVICE_STANDBY requestExemptionResource callback failed. code is: ' + err.code + ',message is: ' + err.message);
} else {
console.log('DEVICE_STANDBY requestExemptionResource callback succeeded.');
}
});
} catch (error) {
console.log('DEVICE_STANDBY requestExemptionResource throw error, code is: ' + error.code + ',message is: ' + error.message);
}
```
## deviceStandby.releaseExemptionResource
releaseExemptionResource(request: ResourceRequest): void;
Cancels exemption for the application.
**System capability**: SystemCapability.ResourceSchedule.DeviceStandby.Exemption
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | -------------------- | ---- | ------------------------------ |
| request |[ResourceRequest](#resourcerequest)| Yes | Request body.|
**Error codes**
For details about the error codes, see [Background Task Management Error Codes](../errorcodes/errorcode-backgroundTaskMgr.md).
| ID | Error Message |
| ---- | --------------------- |
| 9800001 | Memory operation failed. |
| 9800002 | Parcel operation failed. |
| 9800003 | Inner transact failed. |
| 9800004 | System service operation failed. |
| 18700001 | Caller information verification failed. |
**Example**
```js
let resRequest = {
resourceTypes: 1,
uid:10003,
name:"com.demo.app",
duration:10,
reason:"unapply",
};
// Promise mode
try{
deviceStandby.releaseExemptionResource(resRequest).then( () => {
console.log('DEVICE_STANDBY releaseExemptionResource promise succeeded.');
}).catch( err => {
console.log('DEVICE_STANDBY releaseExemptionResource promise failed. code is: ' + err.code + ',message is: ' + err.message);
});
} catch (error) {
console.log('DEVICE_STANDBY releaseExemptionResource throw error, code is: ' + error.code + ',message is: ' + error.message);
}
// Asynchronous callback mode
try{
deviceStandby.releaseExemptionResource(resRequest, (err) => {
if (err) {
console.log('DEVICE_STANDBY releaseExemptionResource callback failed. code is: ' + err.code + ',message is: ' + err.message);
} else {
console.log('DEVICE_STANDBY releaseExemptionResource callback succeeded.');
}
});
} catch (error) {
console.log('DEVICE_STANDBY releaseExemptionResource throw error, code is: ' + error.code + ',message is: ' + error.message);
}
```
## ResourceType
Enumerates the types of resources that can be used by exempted applications.
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**System API**: This is a system API.
|Name |Value |Description|
| ------------ | ------------ |--------------|
|NETWORK |1 |Network access resource.|
|RUNNING_LOCK |2 |CPU running lock resource.|
|TIMER |4 | Timer task resource.|
|WORK_SCHEDULER |8 | Work task resource.|
|AUTO_SYNC |16 | Automatic synchronization resource.|
|PUSH |32 | Push kit resource.|
|FREEZE |64 | Freezing application resource.|
## ExemptedAppInfo
Defines the information about an exempted application.
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**System API**: This is a system API.
|Name |Type | Mandatory |Description |
| ------------ | ------------ |------------ | ------------ |
|[resourceTypes](#resourcetype) | number | Yes |Types of resources that can be used. |
|name |string | Yes | Name of the application. |
|duration | number | Yes | Exemption duration.|
## ResourceRequest
Defines the message used to request to be an exempted application.
**Required permissions**: ohos.permission.DEVICE_STANDBY_EXEMPTION
**System API**: This is a system API.
|Name |Type | Mandatory |Description |
| ------------ | ------------ |------------| ------------ |
|[resourceTypes](#resourcetype) | number | Yes |Types of resources that can be used. |
|uid | number | Yes |UID of the application. |
|name |string | Yes | Name of the application. |
|duration | number | Yes | Exemption duration.|
|reason |string | Yes | Reason for the request. |
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册