Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
69c7d53f
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看板
未验证
提交
69c7d53f
编写于
8月 24, 2023
作者:
朱
朱天怡
提交者:
Gitee
8月 24, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
告警
Signed-off-by:
N
朱天怡
<
zhutianyi2@huawei.com
>
上级
db741516
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
25 addition
and
29 deletion
+25
-29
zh-cn/application-dev/task-management/continuous-task.md
zh-cn/application-dev/task-management/continuous-task.md
+18
-23
zh-cn/application-dev/task-management/work-scheduler.md
zh-cn/application-dev/task-management/work-scheduler.md
+7
-6
未找到文件。
zh-cn/application-dev/task-management/continuous-task.md
浏览文件 @
69c7d53f
...
...
@@ -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
'
...
...
zh-cn/application-dev/task-management/work-scheduler.md
浏览文件 @
69c7d53f
...
...
@@ -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
}
`
);
}
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录