Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
5f504f5f
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
5f504f5f
编写于
1月 17, 2023
作者:
Z
zhangxin_T
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
bgtask doc modify
Signed-off-by:
N
zhangxin_T
<
zhangxin312@huawei.com
>
上级
4721f767
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
113 addition
and
77 deletion
+113
-77
zh-cn/application-dev/reference/apis/js-apis-resourceschedule-backgroundTaskManager.md
...ce/apis/js-apis-resourceschedule-backgroundTaskManager.md
+31
-23
zh-cn/application-dev/task-management/background-task-overview.md
...plication-dev/task-management/background-task-overview.md
+5
-5
zh-cn/application-dev/task-management/continuous-task-dev-guide.md
...lication-dev/task-management/continuous-task-dev-guide.md
+55
-43
zh-cn/application-dev/task-management/efficiency-resources-apply-dev-guide.md
...v/task-management/efficiency-resources-apply-dev-guide.md
+22
-6
zh-cn/application-dev/task-management/figures/bgtask_choice.png
...application-dev/task-management/figures/bgtask_choice.png
+0
-0
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-resourceschedule-backgroundTaskManager.md
浏览文件 @
5f504f5f
...
...
@@ -66,8 +66,8 @@ requestSuspendDelay(reason: string, callback: Callback<void>): DelaySuspen
let
delayInfo
=
backgroundTaskManager
.
requestSuspendDelay
(
myReason
,
()
=>
{
console
.
info
(
"
Request suspension delay will time out.
"
);
})
var
id
=
delayInfo
.
requestId
;
var
time
=
delayInfo
.
actualDelayTime
;
let
id
=
delayInfo
.
requestId
;
let
time
=
delayInfo
.
actualDelayTime
;
console
.
info
(
"
The requestId is:
"
+
id
);
console
.
info
(
"
The actualDelayTime is:
"
+
time
);
}
catch
(
error
)
{
...
...
@@ -257,7 +257,7 @@ startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: Want
```
js
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
wantAgent
from
'
@ohos.wantAgent
'
;
import
wantAgent
from
'
@ohos.
app.ability.
wantAgent
'
;
function
callback
(
error
,
data
)
{
if
(
error
)
{
...
...
@@ -281,14 +281,18 @@ export default class EntryAbility extends UIAbility {
wantAgentFlags
:
[
wantAgent
.
WantAgentFlags
.
UPDATE_PRESENT_FLAG
]
};
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
this
.
context
,
backgroundTaskManager
.
BackgroundMode
.
LOCATION
,
wantAgentObj
,
callback
)
}
catch
(
error
)
{
console
.
error
(
`Operation startBackgroundRunning failed. code is
${
error
.
code
}
message is
${
error
.
message
}
`
);
}
});
try
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
this
.
context
,
backgroundTaskManager
.
BackgroundMode
.
LOCATION
,
wantAgentObj
,
callback
)
}
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
}
`
);
}
}
};
```
...
...
@@ -336,7 +340,7 @@ startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: Want
```
js
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
wantAgent
from
'
@ohos.wantAgent
'
;
import
wantAgent
from
'
@ohos.
app.ability.
wantAgent
'
;
export
default
class
EntryAbility
extends
UIAbility
{
onCreate
(
want
,
launchParam
)
{
...
...
@@ -352,18 +356,22 @@ export default class EntryAbility extends UIAbility {
wantAgentFlags
:
[
wantAgent
.
WantAgentFlags
.
UPDATE_PRESENT_FLAG
]
};
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
this
.
context
,
backgroundTaskManager
.
BackgroundMode
.
LOCATION
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
"
Operation startBackgroundRunning succeeded
"
);
}).
catch
((
error
)
=>
{
try
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
this
.
context
,
backgroundTaskManager
.
BackgroundMode
.
LOCATION
,
wantAgentObj
).
then
(()
=>
{
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
}
`
);
}
);
}
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
}
`
);
}
}
};
```
...
...
zh-cn/application-dev/task-management/background-task-overview.md
浏览文件 @
5f504f5f
...
...
@@ -100,13 +100,13 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业
-
携带参数信息支持number、string、bool三种类型。
## 申请能效资源
能效资源可以分为四种:CPU资源,WORK_SCHEDULER资源,软件资源(COMMON_EVENT,TIMER),硬件资源(GPS,BLUETOOTH,
AUDIO)。
供系统应用使用的能效资源可以分为两类:软件资源(WORK_SCHEDULER, COMMON_EVENT, TIMER),硬件资源(CPU, GPS, BLUETOOTH,
AUDIO)。
应用
或进程申请能效资源后能够获得相应特权
:
应用
申请不同的能效资源后可以执行相应的操作
:
*
申请CPU资源后可以不被挂起,直到任务完成。
*
申请WORK_SCHEDULER资源后不受延迟任务执行频率约束,且任务执行时间增加
*
申请WORK_SCHEDULER资源后不受延迟任务执行频率约束,且任务执行时间增加
。
*
申请COMMON_EVENT资源后,应用在后台处于挂起状态时,仍然能够接收到系统公共事件,申请TIMER资源后,应用能够使用定时器执行精确定时任务、
*
申请
硬件资源后,应用在后台被挂起后,依然能够被相关
服务唤醒,执行相应的任务。
*
申请
(GPS, BLUETOOTH, AUDIO)硬件资源后,应用在后台被挂起后,依然能够被管理相关硬件的
服务唤醒,执行相应的任务。
**表1**
能效资源种类
...
...
@@ -125,4 +125,4 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业
-
能效资源申请或者释放可以由进程或者应用发起,由应用发起的资源释放会释放属于它的同类型的所有资源,包括进程申请的资源。例如应用申请了CPU资源,进程申请了CPU和WORK_SCHEDULER资源,当应用释放CPU资源的时候,会将进程的CPU资源一同释放,同时不同类型的WORK_SCHEDULER资源不受影响。由进程发起的资源释放对应用申请的资源没有影响,例如当应用和进程同时申请了CPU,进程发起了CPU资源释放,应用的CPU资源不会被释放。
-
同时申请同一类持久资源和非持久资源,持久资源会覆盖非持久资源,在超时时不会释放资源。例如应用首先申请了10s的CPU资源,然后在第5s的时候申请了持久的CPU资源,那么资源会变成持久的,非持久的CPU资源记录会被持久化的CPU资源记录覆盖,到了第10s的时候资源不会被释放,如果在第8s的时候提前释放了资源,那么会将CPU资源释放,无法单独释放其中非持久的或者持久的CPU资源。
-
WORK_SCHEDULER资源只能由应用申请和释放,不能由进程申请和释放。
-
需要使用能效资源的应用
,需要向应用中心提出申请,获取
相应的特权。
-
需要使用能效资源的应用
必须是系统应用,同时需要向应用中心提出申请,配置
相应的特权。
zh-cn/application-dev/task-management/continuous-task-dev-guide.md
浏览文件 @
5f504f5f
...
...
@@ -61,7 +61,7 @@ Stage模型的相关信息参考[Stage模型开发概述](../application-models/
2、在应用内执行长时任务时,由于元能力启动管控规则限制,不支持同应用通过startAbilityByCall的形式在后台创建并运行Ability。可以直接在page中,执行相应的代码。Stage模型的Ability使用参考
[
Stage模型开发指导-UIAbility组件
](
../application-models/uiability-overview.md
)
。
```
ts
import
wantAgent
from
'
@ohos.wantAgent
'
;
import
wantAgent
from
'
@ohos.
app.ability.
wantAgent
'
;
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
@
Entry
...
...
@@ -89,18 +89,22 @@ struct Index {
};
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
this
.
context
,
backgroundTaskManager
.
BackgroundMode
.
DATA_TRANSFER
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
"
Operation startBackgroundRunning succeeded
"
);
}).
catch
((
err
)
=>
{
console
.
error
(
"
Operation startBackgroundRunning failed Cause:
"
+
err
);
});
}
catch
(
error
)
{
console
.
error
(
`Operation startBackgroundRunning failed. code is
${
error
.
code
}
message is
${
error
.
message
}
`
);
}
});
try
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
this
.
context
,
backgroundTaskManager
.
BackgroundMode
.
DATA_TRANSFER
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
"
Operation startBackgroundRunning succeeded
"
);
}).
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 getWantAgent failed. code is
${
error
.
code
}
message is
${
error
.
message
}
`
);
}
}
stopContinuousTask
()
{
...
...
@@ -151,8 +155,8 @@ struct Index {
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
wantAgent
from
'
@ohos.wantAgent
'
;
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
wantAgent
from
'
@ohos.
app.ability.
wantAgent
'
;
const
MSG_SEND_METHOD
:
string
=
'
CallSendMsg
'
;
...
...
@@ -176,23 +180,27 @@ function startContinuousTask() {
};
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
featureAbility
.
getContext
(),
backgroundTaskManager
.
BackgroundMode
.
DATA_TRANSFER
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
"
Operation startBackgroundRunning succeeded
"
);
}).
catch
((
error
)
=>
{
try
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
mContext
,
backgroundTaskManager
.
BackgroundMode
.
DATA_TRANSFER
,
wantAgentObj
).
then
(()
=>
{
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
}
`
);
}
);
}
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
()
{
try
{
backgroundTaskManager
.
stopBackgroundRunning
(
featureAbility
.
getContext
()
).
then
(()
=>
{
backgroundTaskManager
.
stopBackgroundRunning
(
mContext
).
then
(()
=>
{
console
.
info
(
"
Operation stopBackgroundRunning succeeded
"
);
}).
catch
((
err
)
=>
{
console
.
error
(
`Operation stopBackgroundRunning failed. code is
${
error
.
code
}
message is
${
error
.
message
}
`
);
...
...
@@ -312,9 +320,9 @@ export default class BgTaskAbility extends UIAbility {
2、在Service Ability调用长时任务的申请和取消接口。
```
js
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
import
wantAgent
from
'
@ohos.wantAgent
'
;
import
wantAgent
from
'
@ohos.
app.ability.
wantAgent
'
;
import
rpc
from
"
@ohos.rpc
"
;
function
startContinuousTask
()
{
...
...
@@ -335,18 +343,22 @@ function startContinuousTask() {
};
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
featureAbility
.
getContext
(),
backgroundTaskManager
.
BackgroundMode
.
DATA_TRANSFER
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
"
Operation startBackgroundRunning succeeded
"
);
}).
catch
((
err
)
=>
{
console
.
error
(
"
Operation startBackgroundRunning failed Cause:
"
+
err
);
});
}
catch
(
error
)
{
console
.
error
(
`Operation startBackgroundRunning failed. code is
${
error
.
code
}
message is
${
error
.
message
}
`
);
}
});
try
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
featureAbility
.
getContext
(),
backgroundTaskManager
.
BackgroundMode
.
DATA_TRANSFER
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
"
Operation startBackgroundRunning succeeded
"
);
}).
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 getWantAgent failed. code is
${
error
.
code
}
message is
${
error
.
message
}
`
);
}
}
function
stopContinuousTask
()
{
...
...
@@ -396,7 +408,7 @@ class MyStub extends rpc.RemoteObject {
}
export
default
{
onStart
(
want
)
{
onStart
()
{
console
.
info
(
'
ServiceAbility onStart
'
);
mMyStub
=
new
MyStub
(
"
ServiceAbility-test
"
);
// 在执行后台长时任前,调用申请接口。
...
...
@@ -416,7 +428,7 @@ export default {
onDisconnect
()
{
console
.
info
(
'
ServiceAbility onDisconnect
'
);
},
onCommand
(
want
,
restart
,
startId
)
{
onCommand
(
want
,
startId
)
{
console
.
info
(
'
ServiceAbility onCommand
'
);
}
};
...
...
zh-cn/application-dev/task-management/efficiency-resources-apply-dev-guide.md
浏览文件 @
5f504f5f
...
...
@@ -23,7 +23,7 @@
2、当资源使用完毕,需要及时释放。支持释放部分资源或全部资源。
```
js
import
backgroundTaskManager
from
'
@ohos.backgroundTaskManager
'
;
import
backgroundTaskManager
from
'
@ohos.
resourceschedule.
backgroundTaskManager
'
;
// 申请能效资源
let
request
=
{
...
...
@@ -35,8 +35,14 @@ let request = {
isPersist
:
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
=
{
...
...
@@ -44,10 +50,20 @@ request = {
isApply
:
false
,
timeOut
:
0
,
reason
:
"
reset
"
,
isPersist
:
true
,
isProcess
:
true
,
};
res
=
backgroundTaskManager
.
applyEfficiencyResources
(
request
);
console
.
info
(
"
the result of request is:
"
+
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
}
`
);
}
// 释放全部资源
backgroundTaskManager
.
resetAllEfficiencyResources
();
try
{
backgroundTaskManager
.
resetAllEfficiencyResources
();
}
catch
(
error
)
{
console
.
error
(
`Operation resetAllEfficiencyResources failed. code is
${
error
.
code
}
message is
${
error
.
message
}
`
);
}
```
zh-cn/application-dev/task-management/figures/bgtask_choice.png
查看替换文件 @
4721f767
浏览文件 @
5f504f5f
38.3 KB
|
W:
|
H:
38.0 KB
|
W:
|
H:
2-up
Swipe
Onion skin
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录