Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
48a70a63
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
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看板
未验证
提交
48a70a63
编写于
8月 25, 2023
作者:
朱
朱天怡
提交者:
Gitee
8月 25, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
告警修复
Signed-off-by:
N
朱天怡
<
zhutianyi2@huawei.com
>
上级
69c7d53f
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
227 addition
and
238 deletion
+227
-238
zh-cn/application-dev/task-management/continuous-task.md
zh-cn/application-dev/task-management/continuous-task.md
+227
-238
未找到文件。
zh-cn/application-dev/task-management/continuous-task.md
浏览文件 @
48a70a63
...
@@ -83,6 +83,7 @@
...
@@ -83,6 +83,7 @@
```
ts
```
ts
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
backgroundTaskManager
from
'
@ohos.resourceschedule.backgroundTaskManager
'
;
import
wantAgent
,
{
WantAgent
}
from
'
@ohos.app.ability.wantAgent
'
;
import
wantAgent
,
{
WantAgent
}
from
'
@ohos.app.ability.wantAgent
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
```
```
4.
申请和取消长时任务。
4.
申请和取消长时任务。
...
@@ -188,13 +189,14 @@
...
@@ -188,13 +189,14 @@
import
window
from
'
@ohos.window
'
;
import
window
from
'
@ohos.window
'
;
import
AbilityConstant
from
'
@ohos.app.ability.AbilityConstant
'
;
import
AbilityConstant
from
'
@ohos.app.ability.AbilityConstant
'
;
import
Want
from
'
@ohos.app.ability.Want
'
;
import
Want
from
'
@ohos.app.ability.Want
'
;
import
rpc
from
'
@ohos.rpc
'
;
const
MSG_SEND_METHOD
:
string
=
'
CallSendMsg
'
const
MSG_SEND_METHOD
:
string
=
'
CallSendMsg
'
let
mContext
=
null
;
let
mContext
:
Context
;
function
startContinuousTask
()
{
function
startContinuousTask
()
{
let
w
antAgentInfo
=
{
let
wantAgentInfo
:
wantAgent
.
W
antAgentInfo
=
{
// 点击通知后,将要执行的动作列表
// 点击通知后,将要执行的动作列表
wants
:
[
wants
:
[
{
{
...
@@ -211,62 +213,54 @@
...
@@ -211,62 +213,54 @@
};
};
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
:
WantAgent
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
mContext
,
backgroundTaskManager
.
startBackgroundRunning
(
mContext
,
backgroundTaskManager
.
BackgroundMode
.
AUDIO_RECORDING
,
wantAgentObj
).
then
(()
=>
{
backgroundTaskManager
.
BackgroundMode
.
AUDIO_RECORDING
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
`Succeeded in operationing startBackgroundRunning.`
);
console
.
info
(
`Succeeded in operationing startBackgroundRunning.`
);
}).
catch
((
er
r
)
=>
{
}).
catch
((
err
:
BusinessErro
r
)
=>
{
console
.
error
(
`Failed to operation startBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
console
.
error
(
`Failed to operation startBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
});
}
catch
(
err
)
{
console
.
error
(
`Failed to operation startBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
});
});
}
}
function
stopContinuousTask
()
{
function
stopContinuousTask
()
{
try
{
backgroundTaskManager
.
stopBackgroundRunning
(
mContext
).
then
(()
=>
{
backgroundTaskManager
.
stopBackgroundRunning
(
mContext
).
then
(()
=>
{
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
}).
catch
((
er
r
)
=>
{
}).
catch
((
err
:
BusinessErro
r
)
=>
{
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
});
}
catch
(
err
)
{
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
}
}
class
MyParcelable
{
class
MyParcelable
{
num
:
number
=
0
;
num
:
number
=
0
;
str
:
String
=
''
;
str
:
String
=
''
;
constructor
(
num
,
string
)
{
constructor
(
num
:
number
,
string
:
string
)
{
this
.
num
=
num
;
this
.
num
=
num
;
this
.
str
=
string
;
this
.
str
=
string
;
}
}
marshalling
(
m
essageSequence
)
{
marshalling
(
messageSequence
:
rpc
.
M
essageSequence
)
{
messageSequence
.
writeInt
(
this
.
num
);
messageSequence
.
writeInt
(
this
.
num
);
messageSequence
.
writeString
(
this
.
str
);
messageSequence
.
writeString
(
this
.
str
);
return
true
;
return
true
;
}
}
unmarshalling
(
m
essageSequence
)
{
unmarshalling
(
messageSequence
:
rpc
.
M
essageSequence
)
{
this
.
num
=
messageSequence
.
readInt
();
this
.
num
=
messageSequence
.
readInt
();
this
.
str
=
messageSequence
.
readString
();
this
.
str
=
messageSequence
.
readString
();
return
true
;
return
true
;
}
}
}
}
function
sendMsgCallback
(
data
)
{
function
sendMsgCallback
(
data
:
rpc
.
MessageSequence
)
{
console
.
info
(
'
BgTaskAbility funcCallBack is called
'
+
data
)
console
.
info
(
'
BgTaskAbility funcCallBack is called
'
+
data
);
let
receivedData
=
new
MyParcelable
(
0
,
''
)
let
receivedData
=
new
MyParcelable
(
0
,
''
);
data
.
readParcelable
(
receivedData
)
data
.
readParcelable
(
receivedData
);
console
.
info
(
`receiveData[
${
receivedData
.
num
}
,
${
receivedData
.
str
}
]`
)
console
.
info
(
`receiveData[
${
receivedData
.
num
}
,
${
receivedData
.
str
}
]`
);
// 可以根据Caller端发送的序列化数据的str值,执行不同的方法。
// 可以根据Caller端发送的序列化数据的str值,执行不同的方法。
if
(
receivedData
.
str
===
'
start_bgtask
'
)
{
if
(
receivedData
.
str
===
'
start_bgtask
'
)
{
startContinuousTask
()
startContinuousTask
();
}
else
if
(
receivedData
.
str
===
'
stop_bgtask
'
)
{
}
else
if
(
receivedData
.
str
===
'
stop_bgtask
'
)
{
stopContinuousTask
();
stopContinuousTask
();
}
}
...
@@ -274,42 +268,44 @@
...
@@ -274,42 +268,44 @@
}
}
export
default
class
BgTaskAbility
extends
UIAbility
{
export
default
class
BgTaskAbility
extends
UIAbility
{
onCreate
(
want
,
l
aunchParam
)
{
onCreate
(
want
:
Want
,
launchParam
:
AbilityConstant
.
L
aunchParam
)
{
console
.
info
(
"
[Demo] BgTaskAbility onCreate
"
)
console
.
info
(
"
[Demo] BgTaskAbility onCreate
"
);
this
.
callee
.
on
(
'
test
'
,
sendMsgCallback
);
this
.
callee
.
on
(
'
test
'
,
sendMsgCallback
);
try
{
try
{
this
.
callee
.
on
(
MSG_SEND_METHOD
,
sendMsgCallback
)
this
.
callee
.
on
(
MSG_SEND_METHOD
,
sendMsgCallback
)
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
`
${
MSG_SEND_METHOD
}
register failed with error
${
JSON
.
stringify
(
error
)}
`
)
console
.
error
(
`
${
MSG_SEND_METHOD
}
register failed with error
${
JSON
.
stringify
(
error
)}
`
);
}
}
mContext
=
this
.
context
;
mContext
=
this
.
context
;
}
}
onDestroy
()
{
onDestroy
()
{
console
.
info
(
'
[Demo] BgTaskAbility onDestroy
'
)
console
.
info
(
'
[Demo] BgTaskAbility onDestroy
'
);
}
}
onWindowStageCreate
(
w
indowStage
)
{
onWindowStageCreate
(
windowStage
:
window
.
W
indowStage
)
{
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageCreate
'
)
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageCreate
'
);
windowStage
.
loadContent
(
"
pages/index
"
).
then
((
data
)
=>
{
windowStage
.
loadContent
(
'
pages/Index
'
,
(
error
,
data
)
=>
{
console
.
info
(
`load content succeed with data
${
JSON
.
stringify
(
data
)}
`
)
if
(
error
.
code
)
{
}).
catch
((
error
)
=>
{
console
.
error
(
`load content failed with error
${
JSON
.
stringify
(
error
)}
`
);
console
.
error
(
`load content failed with error
${
JSON
.
stringify
(
error
)}
`
)
return
;
})
}
console
.
info
(
`load content succeed with data
${
JSON
.
stringify
(
data
)}
`
);
});
}
}
onWindowStageDestroy
()
{
onWindowStageDestroy
()
{
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageDestroy
'
)
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageDestroy
'
);
}
}
onForeground
()
{
onForeground
()
{
console
.
info
(
'
[Demo] BgTaskAbility onForeground
'
)
console
.
info
(
'
[Demo] BgTaskAbility onForeground
'
);
}
}
onBackground
()
{
onBackground
()
{
console
.
info
(
'
[Demo] BgTaskAbility onBackground
'
)
console
.
info
(
'
[Demo] BgTaskAbility onBackground
'
);
}
}
};
};
```
```
...
@@ -352,15 +348,16 @@
...
@@ -352,15 +348,16 @@
```
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
,
{
WantAgent
}
from
'
@ohos.app.ability.wantAgent
'
;
import
rpc
from
"
@ohos.rpc
"
;
import
rpc
from
"
@ohos.rpc
"
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
```
```
4.
申请和取消长时任务。在 ServiceAbility 中,调用 startBackgroundRunning() 接口和 startBackgroundRunning() 接口实现长时任务的申请和取消。
4.
申请和取消长时任务。在 ServiceAbility 中,调用 startBackgroundRunning() 接口和 startBackgroundRunning() 接口实现长时任务的申请和取消。
```
js
```
js
function
startContinuousTask
()
{
function
startContinuousTask
()
{
let
w
antAgentInfo
=
{
let
wantAgentInfo
:
wantAgent
.
W
antAgentInfo
=
{
// 点击通知后,将要执行的动作列表
// 点击通知后,将要执行的动作列表
wants
:
[
wants
:
[
{
{
...
@@ -377,30 +374,22 @@
...
@@ -377,30 +374,22 @@
};
};
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
:
WantAgent
)
=>
{
try
{
backgroundTaskManager
.
startBackgroundRunning
(
featureAbility
.
getContext
(),
backgroundTaskManager
.
startBackgroundRunning
(
featureAbility
.
getContext
(),
backgroundTaskManager
.
BackgroundMode
.
AUDIO_RECORDING
,
wantAgentObj
).
then
(()
=>
{
backgroundTaskManager
.
BackgroundMode
.
AUDIO_RECORDING
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
`Succeeded in operationing startBackgroundRunning.`
);
console
.
info
(
`Succeeded in operationing startBackgroundRunning.`
);
}).
catch
((
er
r
)
=>
{
}).
catch
((
err
:
BusinessErro
r
)
=>
{
console
.
error
(
`Failed to operation startBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
console
.
error
(
`Failed to operation startBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
});
}
catch
(
error
)
{
console
.
error
(
`Failed to operation startBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
});
});
}
}
function
stopContinuousTask
()
{
function
stopContinuousTask
()
{
try
{
backgroundTaskManager
.
stopBackgroundRunning
(
featureAbility
.
getContext
()).
then
(()
=>
{
backgroundTaskManager
.
stopBackgroundRunning
(
featureAbility
.
getContext
()).
then
(()
=>
{
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
}).
catch
((
er
r
)
=>
{
}).
catch
((
err
:
BusinessErro
r
)
=>
{
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
});
}
catch
(
error
)
{
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
}
}
async
function
processAsyncJobs
()
{
async
function
processAsyncJobs
()
{
...
@@ -410,7 +399,7 @@
...
@@ -410,7 +399,7 @@
stopContinuousTask
();
stopContinuousTask
();
}
}
let
m
MyStub
;
let
mMyStub
:
MyStub
;
class
MyStub
extends
rpc
.
RemoteObject
{
class
MyStub
extends
rpc
.
RemoteObject
{
constructor
(
des
)
{
constructor
(
des
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录