diff --git a/zh-cn/application-dev/background-task-management/background-task-dev-guide.md b/zh-cn/application-dev/background-task-management/background-task-dev-guide.md index cccb6652ba5ed6d31b645ee7f4777128e98c2ed3..2047780916748df3d73dabb48f13df84c9960e32 100644 --- a/zh-cn/application-dev/background-task-management/background-task-dev-guide.md +++ b/zh-cn/application-dev/background-task-management/background-task-dev-guide.md @@ -34,34 +34,34 @@ import backgroundTaskManager from '@ohos.backgroundTaskManager'; 1. 申请延迟挂起 -```js -import backgroundTaskManager from '@ohos.backgroundTaskManager'; + ```js + import backgroundTaskManager from '@ohos.backgroundTaskManager'; -let myReason = 'test requestSuspendDelay'; -let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => { - console.info("Request suspension delay will time out."); -}); + let myReason = 'test requestSuspendDelay'; + let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => { + console.info("Request suspension delay will time out."); + }); -var id = delayInfo.requestId;console.info("requestId is: " + id); -``` + var id = delayInfo.requestId;console.info("requestId is: " + id); + ``` 2. 获取进入挂起前的剩余时间 -```js -backgroundTaskManager.getRemainingDelayTime(id).then( res => { - console.log('promise => Operation succeeded. Data: ' + JSON.stringify(res)); -}).catch( err => { - console.log('promise => Operation failed. Cause: ' + err.data); -}); -``` + ```js + backgroundTaskManager.getRemainingDelayTime(id).then( res => { + console.log('promise => Operation succeeded. Data: ' + JSON.stringify(res)); + }).catch( err => { + console.log('promise => Operation failed. Cause: ' + err.data); + }); + ``` 3. 取消延迟挂起 -```js -backgroundTaskManager.cancelSuspendDelay(id); -``` + ```js + backgroundTaskManager.cancelSuspendDelay(id); + ``` ## 开发实例 @@ -127,67 +127,67 @@ ohos.permission.KEEP_BACKGROUND_RUNNING 1. 在config.json文件中配置长时任务权限 -```json -"module": { - "package": "com.example.myapplication", - ..., - "reqPermissions": [ - { - "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" + ```json + "module": { + "package": "com.example.myapplication", + ..., + "reqPermissions": [ + { + "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" + } + ] } - ] -} -``` + ``` -1. 申请长时任务 +2. 申请长时任务 -```js -import backgroundTaskManager from '@ohos.backgroundTaskManager'; -import featureAbility from '@ohos.ability.featureAbility'; -import wantAgent from '@ohos.wantAgent'; + ```js + import backgroundTaskManager from '@ohos.backgroundTaskManager'; + import featureAbility from '@ohos.ability.featureAbility'; + import wantAgent from '@ohos.wantAgent'; -let wantAgentInfo = { - wants: [ - { - bundleName: "com.example.myapplication", - abilityName: "com.example.myapplication.MainAbility" - } - ], - operationType: wantAgent.OperationType.START_ABILITY, - requestCode: 0, - wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESET_FLAG] -}; + let wantAgentInfo = { + wants: [ + { + bundleName: "com.example.myapplication", + abilityName: "com.example.myapplication.MainAbility" + } + ], + operationType: wantAgent.OperationType.START_ABILITY, + requestCode: 0, + wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESET_FLAG] + }; -// 通过wantAgent模块的getWantAgent方法获取WantAgent对象 -wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { - backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(), - backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => { - console.info("Operation succeeded"); - }).catch((err) => { - console.error("Operation failed Cause: " + err); + // 通过wantAgent模块的getWantAgent方法获取WantAgent对象 + wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { + backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(), + backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => { + console.info("Operation succeeded"); + }).catch((err) => { + console.error("Operation failed Cause: " + err); + }); }); -}); - -``` + ``` -2. 停止长时任务 +3. 停止长时任务 -```js -import backgroundTaskManager from '@ohos.backgroundTaskManager'; -import featureAbility from '@ohos.ability.featureAbility'; + ```js + import backgroundTaskManager from '@ohos.backgroundTaskManager'; + import featureAbility from '@ohos.ability.featureAbility'; -backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => { - console.info("Operation succeeded"); -}).catch((err) => { - console.error("Operation failed Cause: " + err); -}); + backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => { + console.info("Operation succeeded"); + }).catch((err) => { + console.error("Operation failed Cause: " + err); + }); -``` + ``` ## 开发实例 当服务启动后,在serviceAbility的onStart回调方法中,调用长时任务的申请接口,声明此服务需要在后台长时运行。在onStop回调方法里,调用长时任务取消接口,声明取消长时任务。 -在service.js文件中: +在service.js文件中: + ```js import backgroundTaskManager from '@ohos.backgroundTaskManager'; import featureAbility from '@ohos.ability.featureAbility'; diff --git a/zh-cn/application-dev/background-task-management/background-task-overview.md b/zh-cn/application-dev/background-task-management/background-task-overview.md index 09d7fbac52464f31f1f3fe1004ea28eb14a05b0f..5486df94b96cf6d9ff82f55012e1fce2b98f6427 100644 --- a/zh-cn/application-dev/background-task-management/background-task-overview.md +++ b/zh-cn/application-dev/background-task-management/background-task-overview.md @@ -59,4 +59,4 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业 - 如果任务结束,应用应主动退出后台模式。若在后台运行期间,系统检测到应用并未使用对应后台模式的资源,则会被挂起(Suspend)。 - 避免不合理地申请后台长时任务,长时任务类型要与应用的业务类型匹配。如表1所示。如果执行的任务和申请的类型不匹配,也会被系统检测到并被挂起(Suspend)。 - 长时任务是为了真正在后台长时间执行某个任务,如果一个应用申请了长时任务,但在实际运行过程中,并未真正运行或执行此类任务时,也会被系统检测到并被挂起(Suspend)。 -- 每个元能力同一时刻只能申请运行一个长时任务。 \ No newline at end of file +- 每个Ability同一时刻只能申请运行一个长时任务。 \ No newline at end of file