未验证 提交 0beddcab 编写于 作者: O openharmony_ci 提交者: Gitee

!12471 翻译完成 12267+12234+11889+11731+11605

Merge pull request !12471 from ester.zhou/TR-12267
...@@ -3,18 +3,46 @@ ...@@ -3,18 +3,46 @@
The **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services. The **systemTimer** module provides system timer features. You can use the APIs of this module to implement the alarm clock and other timer services.
> **NOTE** > **NOTE**
>- The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. >
>- The APIs of this module are system APIs and cannot be called by third-party applications. > - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs provided by this module are system APIs.
## Modules to Import ## Modules to Import
``` ```js
import systemTimer from '@ohos.systemTimer'; import systemTimer from '@ohos.systemTimer';
``` ```
## Constants
Provides the constants that define the supported timer types.
## systemTime.createTimer **System capability**: SystemCapability.MiscServices.Time
| Name | Type | Value | Description |
| ------------------- | ------ | ---- | ---------------------------- |
| TIMER_TYPE_REALTIME | number | 1 | CPU time type. (The start time of the timer cannot be later than the current system time.) |
| TIMER_TYPE_WAKEUP | number | 2 | Wakeup type. |
| TIMER_TYPE_EXACT | number | 4 | Exact type. |
| TIMER_TYPE_IDLE | number | 8 | Idle type (not supported currently).|
## TimerOptions
Defines the initialization options for **createTimer**.
**System capability**: SystemCapability.MiscServices.Time
| Name | Type | Mandatory| Description |
| --------- | --------------------------------- | ---- | ------------------------------------------------------------ |
| type | number | Yes | Timer type.<br>**1**: CPU time type. The start time of the timer cannot be later than the current system time.<br>**2**: wakeup type.<br>**4**: exact type.<br>**5**: idle type (not supported currently).|
| repeat | boolean | Yes | Whether the timer is a repeating timer. The value **true** means that the timer is a repeating timer, and **false** means that the timer is a one-shot timer. |
| interval | number | No | Repeat interval. For a repeating timer, the value must be greater than 5000 ms. For a one-shot timer, the value is **0**.|
| wantAgent | [WantAgent](js-apis-wantAgent.md) | No | **WantAgent** object of the notification to be sent when the timer expires. (An application MainAbility can be started, but not a Service ability.)|
| callback | number | Yes | Callback used to return the timer ID. |
## systemTimer.createTimer
createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void
...@@ -25,37 +53,31 @@ Creates a timer. This API uses an asynchronous callback to return the result. ...@@ -25,37 +53,31 @@ Creates a timer. This API uses an asynchronous callback to return the result.
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | ---------------------------------| ---- | --------------------------------------------------------------------------- | | -------- | ----------------------------- | ---- | ------------------------------------------------------------ |
| options | [TimerOptions](#timeroptions) | Yes | Timer options. | | options | [TimerOptions](#timeroptions) | Yes | Timer initialization options, including the timer type, whether the timer is a repeating timer, interval, and **WantAgent** options.|
| callback | AsyncCallback&lt;number> | Yes | Callback used to return the timer ID. |
**Return value**
| Type | Description |
| ------------------------- | ------------------------------------------------------------ |
| syncCallback&lt;number&gt;| Callback used to return the timer ID. |
**Example** **Example**
```js ```js
export default { export default {
systemTimer () { systemTimer () {
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat: false repeat: false
} };
systemTimer.createTimer(options, (error, data) => { systemTimer.createTimer(options, (error, data) => {
if (error) { if (error) {
console.error(`failed to systemTime.createTimer ` + JSON.stringify(error)); console.error(`Failed to create timer. Cause:` + JSON.stringify(error));
return; return;
} }
console.log(`systemTime.createTimer success data : ` + JSON.stringify(data)); console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data));
}); });
} }
} }
``` ```
## systemTime.createTimer ## systemTimer.createTimer
createTimer(options: TimerOptions): Promise&lt;number&gt; createTimer(options: TimerOptions): Promise&lt;number&gt;
...@@ -66,35 +88,34 @@ Creates a timer. This API uses a promise to return the result. ...@@ -66,35 +88,34 @@ Creates a timer. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | ---------------------------------| ---- | --------------------------------------------------------------------------- | | ------- | ----------------------------- | ---- | ------------------------------------------------------------ |
| options | [TimerOptions](#timeroptions) | Yes | Timer options. | | options | [TimerOptions](#timeroptions) | Yes | Timer initialization options, including the timer type, whether the timer is a repeating timer, interval, and **WantAgent** options.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| --------------------- | ------------------------------------------------------------ | | --------------------- | ----------------------------- |
| Promise&lt;number&gt; | Promise used to return the timer ID. | | Promise&lt;number&gt; | Promise used to return the timer ID.|
**Example** **Example**
```js ```js
export default { export default {
systemTimer () { systemTimer () {
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} };
systemTimer.createTimer(options).then((data) => { systemTimer.createTimer(options).then((data) => {
console.log(`systemTime.createTimer success data : ` + JSON.stringify(data)); console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data));
}).catch((error) => { }).catch((error) => {
console.error(`failed to systemTime.createTimer because ` + JSON.stringify(error)); console.error(`Failed to create timer. Cause:` + JSON.stringify(error));
}); });
} }
} }
``` ```
## systemTime.startTimer ## systemTimer.startTimer
startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&gt;): void startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&gt;): void
...@@ -105,35 +126,34 @@ Starts a timer. This API uses an asynchronous callback to return the result. ...@@ -105,35 +126,34 @@ Starts a timer. This API uses an asynchronous callback to return the result.
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ----------- | --------------------------- | ---- | ------------------------------------------------------------ | | ----------- | ---------------------- | ---- | ------------------------------ |
| timer | number | Yes | ID of the timer. | | timer | number | Yes | ID of the timer. |
| triggerTime | number | Yes | Time when the timer is triggered, in milliseconds. | | triggerTime | number | Yes | Time when the timer is triggered, in milliseconds.|
| callback | AsyncCallback&lt;void> | Yes | Callback used to return the result. |
**Example** **Example**
```js ```js
export default { export default {
systemTimer () { async systemTimer () {
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime, (error, data) => { systemTimer.startTimer(timerId, triggerTime, (error) => {
if (error) { if (error) {
console.error(`failed to systemTime.startTimer ` + JSON.stringify(error)); console.error(`Failed to start timer. Cause:` + JSON.stringify(error));
return; return;
} }
console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
}); });
} }
} }
``` ```
## systemTime.startTimer ## systemTimer.startTimer
startTimer(timer: number, triggerTime: number): Promise&lt;void&gt; startTimer(timer: number, triggerTime: number): Promise&lt;void&gt;
...@@ -144,32 +164,38 @@ Starts a timer. This API uses a promise to return the result. ...@@ -144,32 +164,38 @@ Starts a timer. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ----------- | --------------------------- | ---- | ------------------------------------------------------------ | | ----------- | ------ | ---- | ------------------------------ |
| timer | number | Yes | ID of the timer. | | triggerTime | number | Yes | Time when the timer is triggered, in milliseconds. | | timer | number | Yes | ID of the timer. |
| triggerTime | number | Yes | Time when the timer is triggered, in milliseconds.|
**Return value**
| Type | Description |
| -------------- | ------------------------- |
| Promise\<void> | Promise that returns no value.|
**Example** **Example**
```js ```js
export default { export default {
systemTimer (){ async systemTimer (){
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime).then((data) => { systemTimer.startTimer(timerId, triggerTime).then((data) => {
console.log(`systemTime.startTimer success data : ` + JSON.stringify(data)); console.log(`Succeeded in startting timer. Data:` + JSON.stringify(data));
}).catch((error) => { }).catch((error) => {
console.error(`failed to systemTime.startTimer because ` + JSON.stringify(error)); console.error(`Failed to start timer. Cause:` + JSON.stringify(error));
}); });
} }
} }
``` ```
## systemTime.stopTimer ## systemTimer.stopTimer
stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
...@@ -180,35 +206,34 @@ Stops a timer. This API uses an asynchronous callback to return the result. ...@@ -180,35 +206,34 @@ Stops a timer. This API uses an asynchronous callback to return the result.
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ | | -------- | ---------------------- | ---- | ------------ |
| timer | number | Yes | ID of the timer. | | timer | number | Yes | ID of the timer.|
| callback | AsyncCallback&lt;void> | Yes | Callback used to return the result. |
**Example** **Example**
```js ```js
export default { export default {
systemTimer () { async systemTimer () {
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stoptTimer(timerId, (error, data) => { systemTimer.stopTimer(timerId, (error) => {
if (error) { if (error) {
console.error(`failed to systemTime.startTimer ` + JSON.stringify(error)); console.error(`Failed to stop timer. Cause:` + JSON.stringify(error));
return; return;
} }
console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
}); });
} }
} }
``` ```
## systemTime.stopTimer ## systemTimer.stopTimer
stopTimer(timer: number): Promise&lt;void&gt; stopTimer(timer: number): Promise&lt;void&gt;
...@@ -219,33 +244,38 @@ Stops a timer. This API uses a promise to return the result. ...@@ -219,33 +244,38 @@ Stops a timer. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------- | ---- | ------------------------------------------------------------ | | ------ | ------ | ---- | ------------ |
| timer | number | Yes | ID of the timer. | | timer | number | Yes | ID of the timer.|
**Return value**
| Type | Description |
| -------------- | ------------------------- |
| Promise\<void> | Promise that returns no value.|
**Example** **Example**
```js ```js
export default { export default {
systemTimer (){ async systemTimer (){
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stoptTimer(timerId).then((data) => { systemTimer.stopTimer(timerId).then((data) => {
console.log(`systemTime.startTimer success data : ` + JSON.stringify(data)); console.log(`Succeeded in stopping timer. Data:` + JSON.stringify(data));
}).catch((error) => { }).catch((error) => {
console.error(`failed to systemTime.startTimer because ` + JSON.stringify(error)); console.error(`Failed to stop timer. Cause:` + JSON.stringify(error));
}); });
} }
} }
``` ```
## systemTime.destroyTimer ## systemTimer.destroyTimer
destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
...@@ -256,36 +286,35 @@ Destroys a timer. This API uses an asynchronous callback to return the result. ...@@ -256,36 +286,35 @@ Destroys a timer. This API uses an asynchronous callback to return the result.
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ | | -------- | ---------------------- | ---- | ------------ |
| timer | number | Yes | ID of the timer. | | timer | number | Yes | ID of the timer.|
| callback | AsyncCallback&lt;void> | Yes | Callback used to return the result. |
**Example** **Example**
```js ```js
export default { export default {
systemTimer () { async systemTimer () {
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId) systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId, (error, data) => { systemTimer.destroyTimer(timerId, (error) => {
if (error) { if (error) {
console.error(`failed to systemTime.startTimer ` + JSON.stringify(error)); console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error));
return; return;
} }
console.log(`systemTime.startTimer success data : ` + JSON.stringify(data));
}); });
} }
} }
``` ```
## systemTime.destroyTimer ## systemTimer.destroyTimer
destroyTimer(timer: number): Promise&lt;void&gt; destroyTimer(timer: number): Promise&lt;void&gt;
...@@ -296,42 +325,34 @@ Destroys a timer. This API uses a promise to return the result. ...@@ -296,42 +325,34 @@ Destroys a timer. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------- | ---- | ------------------------------------------------------------ | | ------ | ------ | ---- | ------------ |
| timer | number | Yes | ID of the timer. | | timer | number | Yes | ID of the timer.|
**Return value**
| Type | Description |
| -------------- | ------------------------- |
| Promise\<void> | Promise that returns no value.|
**Example** **Example**
```js ```js
export default { export default {
systemTimer (){ async systemTimer (){
var options = { let options = {
type: systemTimer.TIMER_TYPE_REALTIME, type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false repeat:false
} }
let timerId = systemTimer.createTimer(options) let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime() let triggerTime = new Date().getTime()
triggerTime += 3000 triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime) systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId) systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId, 10000).then((data) => { systemTimer.destroyTimer(timerId).then((data) => {
console.log(`systemTime.startTimer success data : ` + JSON.stringify(data)); console.log(`Succeeded in destroying timer. Data:` + JSON.stringify(data));
}).catch((error) => { }).catch((error) => {
console.error(`failed to systemTime.startTimer because ` + JSON.stringify(error)); console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error));
}); });
} }
} }
``` ```
## TimerOptions
Defines the initialization options for **createTimer**.
**System capability**: SystemCapability.MiscServices.Time
| Name | Type | Mandatory| Description |
| -------- | ------------------| ---- | ------------------------------------------------------------------------------------------------------------------------- |
| type | number | Yes | **const TIMER_TYPE_REALTIME**: sets the timer to the CPU time type. (When the set time is later than the timer startup time, the timer expires.) If it is not specified, the timer is of the wall-time type.<br>**const TIMER_TYPE_WAKEUP**: sets the timer to the wakeup type. If it is not specified, the timer is of the non-wakeup type.<br>**const TIMER_TYPE_EXACT**: sets the timer to the exact type. If it is not specified, the timer is of the non-exact type.<br>**const TIMER_TYPE_IDLE: number**: sets the timer to the idle type. If it is not specified, the timer is of the non-idle type (not yet supported). |
| repeat | boolean | Yes | Whether the timer is a repeating timer. The value **true** means that the timer is a repeating timer, and **false** means that the timer is a one-shot timer. |
| interval | number | No | Repeat interval. For a repeating timer, the value must be greater than 5000 ms. For a one-shot timer, the value is **0**. |
| wantAgent| wantAgent | No | **wantAgent** object of the notification to be sent when the timer expires. (An application MainAbility can be started, but not a Service ability.) |
| callback | number | Yes | Callback used to return the timer ID. |
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册