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
...
@@ -81,8 +81,9 @@
...
@@ -81,8 +81,9 @@
3.
导入模块。
3.
导入模块。
```
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.
申请和取消长时任务。
...
@@ -184,134 +185,129 @@
...
@@ -184,134 +185,129 @@
**跨设备或跨应用**
申请长时任务示例代码如下:
**跨设备或跨应用**
申请长时任务示例代码如下:
```
ts
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
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
()
{
let
wantAgentInfo
=
{
function
startContinuousTask
()
{
// 点击通知后,将要执行的动作列表
let
wantAgentInfo
:
wantAgent
.
WantAgentInfo
=
{
wants
:
[
// 点击通知后,将要执行的动作列表
{
wants
:
[
bundleName
:
"
com.example.myapplication
"
,
{
abilityName
:
"
com.example.myapplication.MainAbility
"
,
bundleName
:
"
com.example.myapplication
"
,
}
abilityName
:
"
com.example.myapplication.MainAbility
"
,
],
}
// 点击通知后,动作类型
],
operationType
:
wantAgent
.
OperationType
.
START_ABILITY
,
// 点击通知后,动作类型
// 使用者自定义的一个私有值
operationType
:
wantAgent
.
OperationType
.
START_ABILITY
,
requestCode
:
0
,
// 使用者自定义的一个私有值
// 点击通知后,动作执行属性
requestCode
:
0
,
wantAgentFlags
:
[
wantAgent
.
WantAgentFlags
.
UPDATE_PRESENT_FLAG
]
// 点击通知后,动作执行属性
};
wantAgentFlags
:
[
wantAgent
.
WantAgentFlags
.
UPDATE_PRESENT_FLAG
]
};
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
try
{
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
:
WantAgent
)
=>
{
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
((
err
)
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
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
()
{
}
backgroundTaskManager
.
stopBackgroundRunning
(
mContext
).
then
(()
=>
{
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
function
stopContinuousTask
()
{
}).
catch
((
err
:
BusinessError
)
=>
{
try
{
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
backgroundTaskManager
.
stopBackgroundRunning
(
mContext
).
then
(()
=>
{
});
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
}
}).
catch
((
err
)
=>
{
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
class
MyParcelable
{
});
num
:
number
=
0
;
}
catch
(
err
)
{
str
:
String
=
''
;
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
}
constructor
(
num
:
number
,
string
:
string
)
{
}
this
.
num
=
num
;
this
.
str
=
string
;
class
MyParcelable
{
}
num
:
number
=
0
;
str
:
String
=
''
;
marshalling
(
messageSequence
:
rpc
.
MessageSequence
)
{
messageSequence
.
writeInt
(
this
.
num
);
constructor
(
num
,
string
)
{
messageSequence
.
writeString
(
this
.
str
);
this
.
num
=
num
;
return
true
;
this
.
str
=
string
;
}
}
unmarshalling
(
messageSequence
:
rpc
.
MessageSequence
)
{
marshalling
(
messageSequence
)
{
this
.
num
=
messageSequence
.
readInt
();
messageSequence
.
writeInt
(
this
.
num
);
this
.
str
=
messageSequence
.
readString
();
messageSequence
.
writeString
(
this
.
str
);
return
true
;
return
true
;
}
}
}
unmarshalling
(
messageSequence
)
{
function
sendMsgCallback
(
data
:
rpc
.
MessageSequence
)
{
this
.
num
=
messageSequence
.
readInt
();
console
.
info
(
'
BgTaskAbility funcCallBack is called
'
+
data
);
this
.
str
=
messageSequence
.
readString
();
let
receivedData
=
new
MyParcelable
(
0
,
''
);
return
true
;
data
.
readParcelable
(
receivedData
);
}
console
.
info
(
`receiveData[
${
receivedData
.
num
}
,
${
receivedData
.
str
}
]`
);
}
// 可以根据Caller端发送的序列化数据的str值,执行不同的方法。
if
(
receivedData
.
str
===
'
start_bgtask
'
)
{
function
sendMsgCallback
(
data
)
{
startContinuousTask
();
console
.
info
(
'
BgTaskAbility funcCallBack is called
'
+
data
)
}
else
if
(
receivedData
.
str
===
'
stop_bgtask
'
)
{
let
receivedData
=
new
MyParcelable
(
0
,
''
)
stopContinuousTask
();
data
.
readParcelable
(
receivedData
)
}
console
.
info
(
`receiveData[
${
receivedData
.
num
}
,
${
receivedData
.
str
}
]`
)
return
new
MyParcelable
(
10
,
'
Callee test
'
);
// 可以根据Caller端发送的序列化数据的str值,执行不同的方法。
}
if
(
receivedData
.
str
===
'
start_bgtask
'
)
{
startContinuousTask
()
export
default
class
BgTaskAbility
extends
UIAbility
{
}
else
if
(
receivedData
.
str
===
'
stop_bgtask
'
)
{
onCreate
(
want
:
Want
,
launchParam
:
AbilityConstant
.
LaunchParam
)
{
stopContinuousTask
();
console
.
info
(
"
[Demo] BgTaskAbility onCreate
"
);
}
this
.
callee
.
on
(
'
test
'
,
sendMsgCallback
);
return
new
MyParcelable
(
10
,
'
Callee test
'
);
}
try
{
this
.
callee
.
on
(
MSG_SEND_METHOD
,
sendMsgCallback
)
export
default
class
BgTaskAbility
extends
UIAbility
{
}
catch
(
error
)
{
onCreate
(
want
,
launchParam
)
{
console
.
error
(
`
${
MSG_SEND_METHOD
}
register failed with error
${
JSON
.
stringify
(
error
)}
`
);
console
.
info
(
"
[Demo] BgTaskAbility onCreate
"
)
}
this
.
callee
.
on
(
'
test
'
,
sendMsgCallback
);
mContext
=
this
.
context
;
}
try
{
this
.
callee
.
on
(
MSG_SEND_METHOD
,
sendMsgCallback
)
onDestroy
()
{
}
catch
(
error
)
{
console
.
info
(
'
[Demo] BgTaskAbility onDestroy
'
);
console
.
error
(
`
${
MSG_SEND_METHOD
}
register failed with error
${
JSON
.
stringify
(
error
)}
`
)
}
}
mContext
=
this
.
context
;
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
}
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageCreate
'
);
onDestroy
()
{
windowStage
.
loadContent
(
'
pages/Index
'
,
(
error
,
data
)
=>
{
console
.
info
(
'
[Demo] BgTaskAbility onDestroy
'
)
if
(
error
.
code
)
{
}
console
.
error
(
`load content failed with error
${
JSON
.
stringify
(
error
)}
`
);
return
;
onWindowStageCreate
(
windowStage
)
{
}
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageCreate
'
)
console
.
info
(
`load content succeed with data
${
JSON
.
stringify
(
data
)}
`
);
});
windowStage
.
loadContent
(
"
pages/index
"
).
then
((
data
)
=>
{
}
console
.
info
(
`load content succeed with data
${
JSON
.
stringify
(
data
)}
`
)
}).
catch
((
error
)
=>
{
onWindowStageDestroy
()
{
console
.
error
(
`load content failed with error
${
JSON
.
stringify
(
error
)}
`
)
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageDestroy
'
);
})
}
}
onForeground
()
{
onWindowStageDestroy
()
{
console
.
info
(
'
[Demo] BgTaskAbility onForeground
'
);
console
.
info
(
'
[Demo] BgTaskAbility onWindowStageDestroy
'
)
}
}
onBackground
()
{
onForeground
()
{
console
.
info
(
'
[Demo] BgTaskAbility onBackground
'
);
console
.
info
(
'
[Demo] BgTaskAbility onForeground
'
)
}
}
};
onBackground
()
{
console
.
info
(
'
[Demo] BgTaskAbility onBackground
'
)
}
};
```
```
...
@@ -350,119 +346,112 @@
...
@@ -350,119 +346,112 @@
3.
导入模块。
3.
导入模块。
```
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
wantAgentInfo
=
{
let
wantAgentInfo
:
wantAgent
.
WantAgentInfo
=
{
// 点击通知后,将要执行的动作列表
// 点击通知后,将要执行的动作列表
wants
:
[
wants
:
[
{
{
bundleName
:
"
com.example.myapplication
"
,
bundleName
:
"
com.example.myapplication
"
,
abilityName
:
"
com.example.myapplication.MainAbility
"
abilityName
:
"
com.example.myapplication.MainAbility
"
}
}
],
],
// 点击通知后,动作类型
// 点击通知后,动作类型
operationType
:
wantAgent
.
OperationType
.
START_ABILITY
,
operationType
:
wantAgent
.
OperationType
.
START_ABILITY
,
// 使用者自定义的一个私有值
// 使用者自定义的一个私有值
requestCode
:
0
,
requestCode
:
0
,
// 点击通知后,动作执行属性
// 点击通知后,动作执行属性
wantAgentFlags
:
[
wantAgent
.
WantAgentFlags
.
UPDATE_PRESENT_FLAG
]
wantAgentFlags
:
[
wantAgent
.
WantAgentFlags
.
UPDATE_PRESENT_FLAG
]
};
};
// 通过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
((
err
:
BusinessError
)
=>
{
}).
catch
((
err
)
=>
{
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
()
{
});
backgroundTaskManager
.
stopBackgroundRunning
(
featureAbility
.
getContext
()).
then
(()
=>
{
}
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
}).
catch
((
err
:
BusinessError
)
=>
{
function
stopContinuousTask
()
{
console
.
error
(
`Failed to operation stopBackgroundRunning. Code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
try
{
});
backgroundTaskManager
.
stopBackgroundRunning
(
featureAbility
.
getContext
()).
then
(()
=>
{
}
console
.
info
(
`Succeeded in operationing stopBackgroundRunning.`
);
}).
catch
((
err
)
=>
{
async
function
processAsyncJobs
()
{
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
}
`
);
stopContinuousTask
();
}
}
}
let
mMyStub
:
MyStub
;
async
function
processAsyncJobs
()
{
// 此处执行具体的长时任务。
class
MyStub
extends
rpc
.
RemoteObject
{
constructor
(
des
)
{
// 长时任务执行完,调用取消接口,释放资源。
if
(
typeof
des
===
'
string
'
)
{
stopContinuousTask
();
super
(
des
);
}
}
else
{
return
null
;
let
mMyStub
;
}
}
class
MyStub
extends
rpc
.
RemoteObject
{
constructor
(
des
)
{
onRemoteRequest
(
code
,
data
,
reply
,
option
)
{
if
(
typeof
des
===
'
string
'
)
{
console
.
log
(
'
ServiceAbility onRemoteRequest called
'
);
super
(
des
);
// code 的具体含义用户自定义
}
else
{
if
(
code
===
1
)
{
return
null
;
// 接收到申请长时任务的请求码
}
startContinuousTask
();
}
// 此处执行具体长时任务
}
else
if
(
code
===
2
)
{
onRemoteRequest
(
code
,
data
,
reply
,
option
)
{
// 接收到取消长时任务的请求码
console
.
log
(
'
ServiceAbility onRemoteRequest called
'
);
stopContinuousTask
();
// code 的具体含义用户自定义
}
else
{
if
(
code
===
1
)
{
console
.
log
(
'
ServiceAbility unknown request code
'
);
// 接收到申请长时任务的请求码
}
startContinuousTask
();
return
true
;
// 此处执行具体长时任务
}
}
else
if
(
code
===
2
)
{
}
// 接收到取消长时任务的请求码
stopContinuousTask
();
export
default
{
}
else
{
onStart
(
want
)
{
console
.
log
(
'
ServiceAbility unknown request code
'
);
console
.
info
(
'
ServiceAbility onStart
'
);
}
mMyStub
=
new
MyStub
(
"
ServiceAbility-test
"
);
return
true
;
// 在执行长时任务前,调用申请接口。
}
startContinuousTask
();
}
processAsyncJobs
();
},
export
default
{
onStop
()
{
onStart
(
want
)
{
console
.
info
(
'
ServiceAbility onStop
'
);
console
.
info
(
'
ServiceAbility onStart
'
);
},
mMyStub
=
new
MyStub
(
"
ServiceAbility-test
"
);
onConnect
(
want
)
{
// 在执行长时任务前,调用申请接口。
console
.
info
(
'
ServiceAbility onConnect
'
);
startContinuousTask
();
return
mMyStub
;
processAsyncJobs
();
},
},
onReconnect
(
want
)
{
onStop
()
{
console
.
info
(
'
ServiceAbility onReconnect
'
);
console
.
info
(
'
ServiceAbility onStop
'
);
},
},
onDisconnect
()
{
onConnect
(
want
)
{
console
.
info
(
'
ServiceAbility onDisconnect
'
);
console
.
info
(
'
ServiceAbility onConnect
'
);
},
return
mMyStub
;
onCommand
(
want
,
restart
,
startId
)
{
},
console
.
info
(
'
ServiceAbility onCommand
'
);
onReconnect
(
want
)
{
}
console
.
info
(
'
ServiceAbility onReconnect
'
);
};
},
onDisconnect
()
{
console
.
info
(
'
ServiceAbility onDisconnect
'
);
},
onCommand
(
want
,
restart
,
startId
)
{
console
.
info
(
'
ServiceAbility onCommand
'
);
}
};
```
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录