未验证 提交 8c99526d 编写于 作者: O openharmony_ci 提交者: Gitee

!13709 后台任务文档修改

Merge pull request !13709 from 张鑫/master
...@@ -66,8 +66,8 @@ requestSuspendDelay(reason: string, callback: Callback<void>): DelaySuspen ...@@ -66,8 +66,8 @@ requestSuspendDelay(reason: string, callback: Callback<void>): DelaySuspen
let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => { let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => {
console.info("Request suspension delay will time out."); console.info("Request suspension delay will time out.");
}) })
var id = delayInfo.requestId; let id = delayInfo.requestId;
var time = delayInfo.actualDelayTime; let time = delayInfo.actualDelayTime;
console.info("The requestId is: " + id); console.info("The requestId is: " + id);
console.info("The actualDelayTime is: " + time); console.info("The actualDelayTime is: " + time);
} catch (error) { } catch (error) {
...@@ -281,14 +281,18 @@ export default class EntryAbility extends UIAbility { ...@@ -281,14 +281,18 @@ export default class EntryAbility extends UIAbility {
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}; };
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { try {
try { wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
backgroundTaskManager.startBackgroundRunning(this.context, try {
backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj, callback) backgroundTaskManager.startBackgroundRunning(this.context,
} catch (error) { backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj, callback)
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); } catch (error) {
} console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}); }
});
} catch (error) {
console.error(`Operation getWantAgent failed. code is ${error.code} message is ${error.message}`);
}
} }
}; };
``` ```
...@@ -352,18 +356,22 @@ export default class EntryAbility extends UIAbility { ...@@ -352,18 +356,22 @@ export default class EntryAbility extends UIAbility {
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}; };
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { try {
try { wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
backgroundTaskManager.startBackgroundRunning(this.context, try {
backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => { backgroundTaskManager.startBackgroundRunning(this.context,
console.info("Operation startBackgroundRunning succeeded"); backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => {
}).catch((error) => { console.info("Operation startBackgroundRunning succeeded");
}).catch((error) => {
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
});
} catch (error) {
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}); }
} catch (error) { });
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); } catch (error) {
} console.error(`Operation getWantAgent failed. code is ${error.code} message is ${error.message}`);
}); }
} }
}; };
``` ```
......
...@@ -100,13 +100,13 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业 ...@@ -100,13 +100,13 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业
- 携带参数信息支持number、string、bool三种类型。 - 携带参数信息支持number、string、bool三种类型。
## 申请能效资源 ## 申请能效资源
能效资源可以分为四种:CPU资源,WORK_SCHEDULER资源,软件资源(COMMON_EVENT,TIMER),硬件资源(GPS,BLUETOOTH,AUDIO)。 供系统应用使用的能效资源可以分为两类:软件资源(WORK_SCHEDULER, COMMON_EVENT, TIMER),硬件资源(CPU, GPS, BLUETOOTH, AUDIO)。
应用或进程申请能效资源后能够获得相应特权 应用申请不同的能效资源后可以执行相应的操作
* 申请CPU资源后可以不被挂起,直到任务完成。 * 申请CPU资源后可以不被挂起,直到任务完成。
* 申请WORK_SCHEDULER资源后不受延迟任务执行频率约束,且任务执行时间增加 * 申请WORK_SCHEDULER资源后不受延迟任务执行频率约束,且任务执行时间增加
* 申请COMMON_EVENT资源后,应用在后台处于挂起状态时,仍然能够接收到系统公共事件,申请TIMER资源后,应用能够使用定时器执行精确定时任务 * 申请COMMON_EVENT资源后,应用在后台处于挂起状态时,仍然能够接收到系统公共事件,申请TIMER资源后,应用能够使用定时器执行精确定时任务
* 申请硬件资源后,应用在后台被挂起后,依然能够被相关服务唤醒,执行相应的任务。 * 申请资源(GPS, BLUETOOTH, AUDIO)后,应用在后台被挂起后,依然能够被管理相关硬件的服务唤醒,执行相应的任务。
**表1** 能效资源种类 **表1** 能效资源种类
...@@ -125,4 +125,4 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业 ...@@ -125,4 +125,4 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业
- 能效资源申请或者释放可以由进程或者应用发起,由应用发起的资源释放会释放属于它的同类型的所有资源,包括进程申请的资源。例如应用申请了CPU资源,进程申请了CPU和WORK_SCHEDULER资源,当应用释放CPU资源的时候,会将进程的CPU资源一同释放,同时不同类型的WORK_SCHEDULER资源不受影响。由进程发起的资源释放对应用申请的资源没有影响,例如当应用和进程同时申请了CPU,进程发起了CPU资源释放,应用的CPU资源不会被释放。 - 能效资源申请或者释放可以由进程或者应用发起,由应用发起的资源释放会释放属于它的同类型的所有资源,包括进程申请的资源。例如应用申请了CPU资源,进程申请了CPU和WORK_SCHEDULER资源,当应用释放CPU资源的时候,会将进程的CPU资源一同释放,同时不同类型的WORK_SCHEDULER资源不受影响。由进程发起的资源释放对应用申请的资源没有影响,例如当应用和进程同时申请了CPU,进程发起了CPU资源释放,应用的CPU资源不会被释放。
- 同时申请同一类持久资源和非持久资源,持久资源会覆盖非持久资源,在超时时不会释放资源。例如应用首先申请了10s的CPU资源,然后在第5s的时候申请了持久的CPU资源,那么资源会变成持久的,非持久的CPU资源记录会被持久化的CPU资源记录覆盖,到了第10s的时候资源不会被释放,如果在第8s的时候提前释放了资源,那么会将CPU资源释放,无法单独释放其中非持久的或者持久的CPU资源。 - 同时申请同一类持久资源和非持久资源,持久资源会覆盖非持久资源,在超时时不会释放资源。例如应用首先申请了10s的CPU资源,然后在第5s的时候申请了持久的CPU资源,那么资源会变成持久的,非持久的CPU资源记录会被持久化的CPU资源记录覆盖,到了第10s的时候资源不会被释放,如果在第8s的时候提前释放了资源,那么会将CPU资源释放,无法单独释放其中非持久的或者持久的CPU资源。
- WORK_SCHEDULER资源只能由应用申请和释放,不能由进程申请和释放。 - WORK_SCHEDULER资源只能由应用申请和释放,不能由进程申请和释放。
- 需要使用能效资源的应用,需要向应用中心提出申请,获取相应的特权。 - 需要使用能效资源的应用必须是系统应用,同时需要向应用中心提出申请,配置相应的特权。
...@@ -89,18 +89,22 @@ struct Index { ...@@ -89,18 +89,22 @@ struct Index {
}; };
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象 // 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { try {
try { wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
backgroundTaskManager.startBackgroundRunning(this.context, try {
backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => { backgroundTaskManager.startBackgroundRunning(this.context,
console.info("Operation startBackgroundRunning succeeded"); backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => {
}).catch((err) => { console.info("Operation startBackgroundRunning succeeded");
console.error("Operation startBackgroundRunning failed Cause: " + err); }).catch((err) => {
}); console.error("Operation startBackgroundRunning failed Cause: " + err);
} catch (error) { });
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); } catch (error) {
} console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}); }
});
} catch (error) {
console.error(`Operation getWantAgent failed. code is ${error.code} message is ${error.message}`);
}
} }
stopContinuousTask() { stopContinuousTask() {
...@@ -151,7 +155,7 @@ struct Index { ...@@ -151,7 +155,7 @@ struct Index {
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager'; import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import wantAgent from '@ohos.app.ability.wantAgent'; import wantAgent from '@ohos.app.ability.wantAgent';
const MSG_SEND_METHOD: string = 'CallSendMsg'; const MSG_SEND_METHOD: string = 'CallSendMsg';
...@@ -176,18 +180,22 @@ function startContinuousTask() { ...@@ -176,18 +180,22 @@ function startContinuousTask() {
}; };
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象 // 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { try {
try { wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
backgroundTaskManager.startBackgroundRunning(mContext, try {
backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => { backgroundTaskManager.startBackgroundRunning(mContext,
console.info("Operation startBackgroundRunning succeeded"); backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => {
}).catch((error) => { console.info("Operation startBackgroundRunning succeeded");
}).catch((error) => {
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
});
} catch (error) {
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}); }
} catch (error) { });
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); } catch (error) {
} console.error(`Operation getWantAgent failed. code is ${error.code} message is ${error.message}`);
}); }
} }
function stopContinuousTask() { function stopContinuousTask() {
...@@ -312,7 +320,7 @@ export default class BgTaskAbility extends UIAbility { ...@@ -312,7 +320,7 @@ export default class BgTaskAbility extends UIAbility {
2、在Service Ability调用长时任务的申请和取消接口。 2、在Service Ability调用长时任务的申请和取消接口。
```js ```js
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager'; import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import wantAgent from '@ohos.app.ability.wantAgent'; import wantAgent from '@ohos.app.ability.wantAgent';
import rpc from "@ohos.rpc"; import rpc from "@ohos.rpc";
...@@ -335,18 +343,22 @@ function startContinuousTask() { ...@@ -335,18 +343,22 @@ function startContinuousTask() {
}; };
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象 // 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { try {
try { wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(), try {
backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => { backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
console.info("Operation startBackgroundRunning succeeded"); backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => {
}).catch((err) => { console.info("Operation startBackgroundRunning succeeded");
console.error("Operation startBackgroundRunning failed Cause: " + err); }).catch((err) => {
}); console.error("Operation startBackgroundRunning failed Cause: " + err);
} catch (error) { });
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); } catch (error) {
} console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}); }
});
} catch (error) {
console.error(`Operation getWantAgent failed. code is ${error.code} message is ${error.message}`);
}
} }
function stopContinuousTask() { function stopContinuousTask() {
...@@ -396,7 +408,7 @@ class MyStub extends rpc.RemoteObject { ...@@ -396,7 +408,7 @@ class MyStub extends rpc.RemoteObject {
} }
export default { export default {
onStart(want) { onStart() {
console.info('ServiceAbility onStart'); console.info('ServiceAbility onStart');
mMyStub = new MyStub("ServiceAbility-test"); mMyStub = new MyStub("ServiceAbility-test");
// 在执行后台长时任前,调用申请接口。 // 在执行后台长时任前,调用申请接口。
...@@ -416,7 +428,7 @@ export default { ...@@ -416,7 +428,7 @@ export default {
onDisconnect() { onDisconnect() {
console.info('ServiceAbility onDisconnect'); console.info('ServiceAbility onDisconnect');
}, },
onCommand(want, restart, startId) { onCommand(want, startId) {
console.info('ServiceAbility onCommand'); console.info('ServiceAbility onCommand');
} }
}; };
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
2、当资源使用完毕,需要及时释放。支持释放部分资源或全部资源。 2、当资源使用完毕,需要及时释放。支持释放部分资源或全部资源。
```js ```js
import backgroundTaskManager from '@ohos.backgroundTaskManager'; import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
// 申请能效资源 // 申请能效资源
let request = { let request = {
...@@ -35,8 +35,14 @@ let request = { ...@@ -35,8 +35,14 @@ let request = {
isPersist: true, isPersist: true,
isProcess: true, isProcess: true,
}; };
let res = backgroundTaskManager.applyEfficiencyResources(request);
console.info("the result of request is: " + res); let res;
try {
res = backgroundTaskManager.applyEfficiencyResources(request);
console.info("the result of request is: " + res);
} catch (error) {
console.error(`Operation applyEfficiencyResources failed. code is ${error.code} message is ${error.message}`);
}
// 释放部分资源 // 释放部分资源
request = { request = {
...@@ -44,10 +50,20 @@ request = { ...@@ -44,10 +50,20 @@ request = {
isApply: false, isApply: false,
timeOut: 0, timeOut: 0,
reason: "reset", reason: "reset",
isPersist: true,
isProcess: true,
}; };
res = backgroundTaskManager.applyEfficiencyResources(request); try {
console.info("the result of request is: " + res); res = backgroundTaskManager.applyEfficiencyResources(request);
console.info("the result of request is: " + res);
} catch (error) {
console.error(`Operation applyEfficiencyResources failed. code is ${error.code} message is ${error.message}`);
}
// 释放全部资源 // 释放全部资源
backgroundTaskManager.resetAllEfficiencyResources(); try {
backgroundTaskManager.resetAllEfficiencyResources();
} catch (error) {
console.error(`Operation resetAllEfficiencyResources failed. code is ${error.code} message is ${error.message}`);
}
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册