未验证 提交 27ef199a 编写于 作者: O openharmony_ci 提交者: Gitee

!13058 add time interface

Merge pull request !13058 from wTong888/master
......@@ -35,7 +35,7 @@ import systemTimer from '@ohos.systemTimer';
| 名称 | 类型 | 必填 | 说明 |
| --------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| type | number | 是 | 定时器类型。<br>取值为1时,表示为系统启动时间定时器(定时器启动时间不能晚于当前设置的系统时间) ;<br>取值为2时,表示为唤醒定时器;<br>取值为4时,表示为精准定时器;<br>取值为5时,表示为IDLE模式定时器(暂不支持)。 |
| type | number | 是 | 定时器类型。<br>取值为1,表示为系统启动时间定时器(定时器启动时间不能晚于当前设置的系统时间) ;<br>取值为2,表示为唤醒定时器;<br>取值为4,表示为精准定时器;<br>取值为8,表示为IDLE模式定时器(暂不支持)。 |
| repeat | boolean | 是 | true为循环定时器,false为单次定时器。 |
| interval | number | 否 | 如果是循环定时器,repeat值应大于5000毫秒,非重复定时器置为0。 |
| wantAgent | [WantAgent](js-apis-app-ability-wantAgent.md) | 否 | 设置通知的WantAgent,定时器到期后通知。(支持拉起应用MainAbility,暂不支持拉起ServiceAbility。) |
......@@ -48,6 +48,8 @@ createTimer(options: TimerOptions, callback: AsyncCallback&lt;number&gt;): void
创建定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -66,13 +68,17 @@ export default {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat: false
};
systemTimer.createTimer(options, (error, data) => {
if (error) {
console.error(`Failed to create timer. Cause:` + JSON.stringify(error));
return;
}
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data));
});
try {
systemTimer.createTimer(options, (error) => {
if (error) {
console.info(`Failed to create timer. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in creating timer.`);
});
} catch(e) {
console.info(`Failed to create timer. message:${e.message}, code:${e.code}`);
}
}
}
```
......@@ -83,6 +89,8 @@ createTimer(options: TimerOptions): Promise&lt;number&gt;
创建定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -105,12 +113,16 @@ export default {
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
};
systemTimer.createTimer(options).then((data) => {
console.log(`Succeeded in creating timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to create timer. Cause:` + JSON.stringify(error));
});
};
try {
systemTimer.createTimer(options).then(() => {
console.info(`Succeeded in creating timer.`);
}).catch((error) => {
console.info(`Failed to create timer. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to create timer. message:${e.message}, code:${e.code}`);
}
}
}
```
......@@ -121,6 +133,8 @@ startTimer(timer: number, triggerTime: number, callback: AsyncCallback&lt;void&g
开始定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -143,12 +157,17 @@ export default {
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime, (error) => {
if (error) {
console.error(`Failed to start timer. Cause:` + JSON.stringify(error));
return;
}
});
try {
systemTimer.startTimer(timerId, triggerTime, (error) => {
if (error) {
console.info(`Failed to start timer. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in starting timer.`);
});
} catch(e) {
console.info(`Failed to start timer. message:${e.message}, code:${e.code}`);
}
}
}
```
......@@ -159,6 +178,8 @@ startTimer(timer: number, triggerTime: number): Promise&lt;void&gt;
开始定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -186,11 +207,15 @@ export default {
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime).then((data) => {
console.log(`Succeeded in startting timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to start timer. Cause:` + JSON.stringify(error));
});
try {
systemTimer.startTimer(timerId, triggerTime).then(() => {
console.info(`Succeeded in starting timer.`);
}).catch((error) => {
console.info(`Failed to start timer. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to start timer. message:${e.message}, code:${e.code}`);
}
}
}
```
......@@ -201,6 +226,8 @@ stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
停止定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -214,22 +241,27 @@ stopTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
```js
export default {
async systemTimer () {
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId, (error) => {
if (error) {
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error));
return;
}
});
}
async systemTimer () {
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
try {
systemTimer.stopTimer(timerId, (error) => {
if (error) {
console.info(`Failed to stop timer. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in stopping timer.`);
});
} catch(e) {
console.info(`Failed to stop timer. message:${e.message}, code:${e.code}`);
}
}
}
```
......@@ -239,6 +271,8 @@ stopTimer(timer: number): Promise&lt;void&gt;
停止定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -257,21 +291,25 @@ stopTimer(timer: number): Promise&lt;void&gt;
```js
export default {
async systemTimer (){
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId).then((data) => {
console.log(`Succeeded in stopping timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to stop timer. Cause:` + JSON.stringify(error));
});
}
async systemTimer (){
let options = {
type: systemTimer.TIMER_TYPE_REALTIME,
repeat:false
}
let timerId = await systemTimer.createTimer(options)
let triggerTime = new Date().getTime()
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
try {
systemTimer.stopTimer(timerId).then(() => {
console.info(`Succeeded in stopping timer.`);
}).catch((error) => {
console.info(`Failed to stop timer. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to stop timer. message:${e.message}, code:${e.code}`);
}
}
}
```
......@@ -281,6 +319,8 @@ destroyTimer(timer: number, callback: AsyncCallback&lt;void&gt;): void
销毁定时器,使用callback异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -304,12 +344,17 @@ export default {
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId, (error) => {
if (error) {
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error));
return;
}
});
try {
systemTimer.destroyTimer(timerId, (error) => {
if (error) {
console.info(`Failed to destroy timer. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in destroying timer.`);
});
} catch(e) {
console.info(`Failed to destroying timer. message:${e.message}, code:${e.code}`);
}
}
}
```
......@@ -320,6 +365,8 @@ destroyTimer(timer: number): Promise&lt;void&gt;
销毁定时器,使用Promise异步回调。
**系统接口:** 此接口为系统接口
**系统能力:** SystemCapability.MiscServices.Time
**参数:**
......@@ -348,11 +395,15 @@ export default {
triggerTime += 3000
systemTimer.startTimer(timerId, triggerTime)
systemTimer.stopTimer(timerId)
systemTimer.destroyTimer(timerId).then((data) => {
console.log(`Succeeded in destroying timer. Data:` + JSON.stringify(data));
}).catch((error) => {
console.error(`Failed to destroy timer. Cause:` + JSON.stringify(error));
});
try {
systemTimer.destroyTimer(timerId).then(() => {
console.info(`Succeeded in destroying timer.`);
}).catch((error) => {
console.info(`Failed to destroy timer. message:${error.message}, code:${error.code}`);
});
} catch(e) {
console.info(`Failed to destroying timer. message:${e.message}, code:${e.code}`);
}
}
}
```
\ No newline at end of file
# 时间时区子系统ChangeLog
## cl.time.1 接口异常抛出变更
时间时区子系统时间相关接口异常抛出:201权限否定异常、202非系统应用异常和401参数无效异常。
**变更影响**
基于此前版本开发的应用,继续使用无影响,使用新接口需要捕获并处理抛出的新异常。
**关键接口/组件变更**
变更前:
- 接口异常抛出message,错误码-1。
变更后:
- 接口异常抛出message和code,包括201权限否定异常、202非系统应用异常和401参数无效异常。
原接口中标记为废弃的接口,可以使用新接口中的同名接口替换
| 原接口 | 新接口 |
| ---------------- | -------------------- |
| @ohos.systemTime | @ohos.systemDateTime |
**适配指导**
应用中调用systemTime所有接口可参考下列代码进行异常捕获,以setTime接口为例,其他接口适配方法相同。
callback形式调用:
**示例:**
```js
import systemDateTime from @ohos.systemDateTime
// time对应的时间为2021-01-20 02:36:25
let time = 1611081385000;
try {
systemDateTime.setTime(time, (error) => {
//捕获权限否定异常和非系统应用异常
if (error) {
console.info(`Failed to setting time. message:${error.message}, code:${error.code}`);
return;
}
console.info(`Succeeded in setting time.`);
})
} catch(e) {
//捕获参数校验失败异常
console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
```
promise形式调用:
**示例:**
```js
import systemDateTime from @ohos.systemDateTime
// time对应的时间为2021-01-20 02:36:25
let time = 1611081385000;
try {
systemDateTime.setTime(time).then(() => {
console.info(`Succeeded in setting time.`);
}).catch((error) => {
//捕获权限否定异常和非系统应用异常
console.info(`Failed to setting time. message:${error.message}, code:${error.code}`);
});
} catch(e) {
//捕获参数校验失败异常
console.info(`Failed to set time. message:${e.message}, code:${e.code}`);
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册