diff --git a/zh-cn/application-dev/task-management/continuous-task.md b/zh-cn/application-dev/task-management/continuous-task.md index c027b5b4bb68cdb95319c94e54b66058366e8fd3..435ae6921da947407217f3d4baf604c9ecc165de 100644 --- a/zh-cn/application-dev/task-management/continuous-task.md +++ b/zh-cn/application-dev/task-management/continuous-task.md @@ -82,7 +82,7 @@ ```ts import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager'; - import wantAgent from '@ohos.app.ability.wantAgent'; + import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent'; ``` 4. 申请和取消长时任务。 @@ -99,10 +99,10 @@ struct Index { @State message: string = 'ContinuousTask'; // 通过getContext方法,来获取page所在的UIAbility上下文。 - private context = getContext(this); + private context: Context = getContext(this); startContinuousTask() { - let wantAgentInfo = { + let wantAgentInfo: wantAgent.wantAgentInfo = { // 点击通知后,将要执行的动作列表 wants: [ { @@ -119,30 +119,22 @@ }; // 通过wantAgent模块下getWantAgent方法获取WantAgent对象 - wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { - try { - backgroundTaskManager.startBackgroundRunning(this.context, - backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, wantAgentObj).then(() => { - console.info(`Succeeded in operationing startBackgroundRunning.`); - }).catch((err) => { - console.error(`Failed to operation startBackgroundRunning. Code is ${err.code}, message is ${err.message}`); - }); - } catch (error) { - console.error(`Failed to start background running. Code is ${error.code} message is ${error.message}`); - } + wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => { + backgroundTaskManager.startBackgroundRunning(this.context, + backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, wantAgentObj).then(() => { + console.info(`Succeeded in operationing startBackgroundRunning.`); + }).catch((err: BusinessError) => { + console.error(`Failed to operation startBackgroundRunning. Code is ${err.code}, message is ${err.message}`); + }); }); } stopContinuousTask() { - try { - backgroundTaskManager.stopBackgroundRunning(this.context).then(() => { - console.info(`Succeeded in operationing stopBackgroundRunning.`); - }).catch((err) => { - console.error(`Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}`); - }); - } catch (error) { - console.error(`Failed to stop background running. Code is ${error.code} message is ${error.message}`); - } + backgroundTaskManager.stopBackgroundRunning(this.context).then(() => { + console.info(`Succeeded in operationing stopBackgroundRunning.`); + }).catch((err: BusinessError) => { + console.error(`Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}`); + }); } build() { @@ -193,6 +185,9 @@ ```ts import UIAbility from '@ohos.app.ability.UIAbility'; + import window from '@ohos.window'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + import Want from '@ohos.app.ability.Want'; const MSG_SEND_METHOD: string = 'CallSendMsg' diff --git a/zh-cn/application-dev/task-management/work-scheduler.md b/zh-cn/application-dev/task-management/work-scheduler.md index 484f68b5b93cf0b33a2e8e9cf0f86a2c3181694f..151854ae89fdd2cfe5e25900b33e89ca72b39abe 100644 --- a/zh-cn/application-dev/task-management/work-scheduler.md +++ b/zh-cn/application-dev/task-management/work-scheduler.md @@ -121,6 +121,7 @@ WorkInfo参数用于设置应用条件,参数设置时需遵循以下规则: ```ts import WorkSchedulerExtensionAbility from '@ohos.WorkSchedulerExtensionAbility'; + import workScheduler fron '@ohos.resourceschedule.workScheduler'; ``` 3. 实现WorkSchedulerExtension生命周期接口。 @@ -128,12 +129,12 @@ WorkInfo参数用于设置应用条件,参数设置时需遵循以下规则: ```ts export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility { // 延迟任务开始回调 - onWorkStart(workInfo) { + onWorkStart(workInfo: workScheduler.WorkInfo) { console.info(`onWorkStart, workInfo = ${JSON.stringify(workInfo)}`); } // 延迟任务结束回调 - onWorkStop(workInfo) { + onWorkStop(workInfo: workScheduler.WorkInfo) { console.info(`onWorkStop, workInfo is ${JSON.stringify(workInfo)}`); } } @@ -173,7 +174,7 @@ WorkInfo参数用于设置应用条件,参数设置时需遵循以下规则: 2. 申请延迟任务。 ```ts - private workInfo = { + private workInfo: workScheduler.WorkInfo = { workId: 1, networkType: workScheduler.NetworkType.NETWORK_TYPE_WIFI, bundleName: 'com.example.application', @@ -183,7 +184,7 @@ WorkInfo参数用于设置应用条件,参数设置时需遵循以下规则: try { workScheduler.startWork(this.workInfo); console.info(`startWork success`); - } catch (error) { + } catch (error: BusinessError) { console.error(`startWork failed. code is ${error.code} message is ${error.message}`); } ``` @@ -191,7 +192,7 @@ WorkInfo参数用于设置应用条件,参数设置时需遵循以下规则: 3. 取消延迟任务。 ```ts - private workInfo = { + private workInfo: workScheduler.workInfo = { workId: 1, networkType: workScheduler.NetworkType.NETWORK_TYPE_WIFI, bundleName: 'com.example.application', @@ -201,7 +202,7 @@ WorkInfo参数用于设置应用条件,参数设置时需遵循以下规则: try { workScheduler.stopWork(this.workInfo); console.info(`stopWork success`); - } catch (error) { + } catch (error: BusinessError) { console.error(`stopWork failed. code is ${error.code} message is ${error.message}`); } ```