Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
0d9c4a4e
D
Docs
项目概览
OpenHarmony
/
Docs
12 个月 前同步成功
通知
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0d9c4a4e
编写于
2月 14, 2022
作者:
Z
zhangxin_T
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix white space problem
Signed-off-by:
N
zhangxin_T
<
zhangxin312@huawei.com
>
上级
977381ad
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
66 addition
and
66 deletion
+66
-66
zh-cn/application-dev/background-task-management/background-task-dev-guide.md
...v/background-task-management/background-task-dev-guide.md
+65
-65
zh-cn/application-dev/background-task-management/background-task-overview.md
...ev/background-task-management/background-task-overview.md
+1
-1
未找到文件。
zh-cn/application-dev/background-task-management/background-task-dev-guide.md
浏览文件 @
0d9c4a4e
...
...
@@ -34,34 +34,34 @@ import backgroundTaskManager from '@ohos.backgroundTaskManager';
1.
申请延迟挂起
```
js
import
backgroundTaskManager
from
'
@ohos.backgroundTaskManager
'
;
```js
import backgroundTaskManager from '@ohos.backgroundTaskManager';
let
myReason
=
'
test requestSuspendDelay
'
;
let
delayInfo
=
backgroundTaskManager
.
requestSuspendDelay
(
myReason
,
()
=>
{
console
.
info
(
"
Request suspension delay will time out.
"
);
});
let myReason = 'test requestSuspendDelay';
let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => {
console.info("Request suspension delay will time out.");
});
var
id
=
delayInfo
.
requestId
;
console
.
info
(
"
requestId is:
"
+
id
);
```
var id = delayInfo.requestId;console.info("requestId is: " + id);
```
2.
获取进入挂起前的剩余时间
```
js
backgroundTaskManager
.
getRemainingDelayTime
(
id
).
then
(
res
=>
{
console
.
log
(
'
promise => Operation succeeded. Data:
'
+
JSON
.
stringify
(
res
));
}).
catch
(
err
=>
{
console
.
log
(
'
promise => Operation failed. Cause:
'
+
err
.
data
);
});
```
```js
backgroundTaskManager.getRemainingDelayTime(id).then( res => {
console.log('promise => Operation succeeded. Data: ' + JSON.stringify(res));
}).catch( err => {
console.log('promise => Operation failed. Cause: ' + err.data);
});
```
3.
取消延迟挂起
```
js
backgroundTaskManager
.
cancelSuspendDelay
(
id
);
```
```js
backgroundTaskManager.cancelSuspendDelay(id);
```
## 开发实例
...
...
@@ -127,67 +127,67 @@ ohos.permission.KEEP_BACKGROUND_RUNNING
1.
在config.json文件中配置长时任务权限
```
json
"module"
:
{
"package"
:
"com.example.myapplication"
,
...
,
"reqPermissions"
:
[
{
"name"
:
"ohos.permission.KEEP_BACKGROUND_RUNNING"
```json
"module": {
"package": "com.example.myapplication",
...,
"reqPermissions": [
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING"
}
]
}
]
}
```
```
1
.
申请长时任务
2
.
申请长时任务
```
js
import
backgroundTaskManager
from
'
@ohos.backgroundTaskManager
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
import
wantAgent
from
'
@ohos.wantAgent
'
;
```js
import backgroundTaskManager from '@ohos.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility';
import wantAgent from '@ohos.wantAgent';
let
wantAgentInfo
=
{
wants
:
[
{
bundleName
:
"
com.example.myapplication
"
,
abilityName
:
"
com.example.myapplication.MainAbility
"
}
],
operationType
:
wantAgent
.
OperationType
.
START_ABILITY
,
requestCode
:
0
,
wantAgentFlags
:
[
wantAgent
.
WantAgentFlags
.
UPDATE_PRESET_FLAG
]
};
let wantAgentInfo = {
wants: [
{
bundleName: "com.example.myapplication",
abilityName: "com.example.myapplication.MainAbility"
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESET_FLAG]
};
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent
.
getWantAgent
(
wantAgentInfo
).
then
((
wantAgentObj
)
=>
{
backgroundTaskManager
.
startBackgroundRunning
(
featureAbility
.
getContext
(),
backgroundTaskManager
.
BackgroundMode
.
DATA_TRANSFER
,
wantAgentObj
).
then
(()
=>
{
console
.
info
(
"
Operation succeeded
"
);
}).
catch
((
err
)
=>
{
console
.
error
(
"
Operation failed Cause:
"
+
err
);
// 通过wantAgent模块的getWantAgent方法获取WantAgent对象
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => {
console.info("Operation succeeded");
}).catch((err) => {
console.error("Operation failed Cause: " + err);
});
});
});
```
```
2
.
停止长时任务
3
.
停止长时任务
```
js
import
backgroundTaskManager
from
'
@ohos.backgroundTaskManager
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
```js
import backgroundTaskManager from '@ohos.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility';
backgroundTaskManager
.
stopBackgroundRunning
(
featureAbility
.
getContext
()).
then
(()
=>
{
console
.
info
(
"
Operation succeeded
"
);
}).
catch
((
err
)
=>
{
console
.
error
(
"
Operation failed Cause:
"
+
err
);
});
backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => {
console.info("Operation succeeded");
}).catch((err) => {
console.error("Operation failed Cause: " + err);
});
```
```
## 开发实例
当服务启动后,在serviceAbility的onStart回调方法中,调用长时任务的申请接口,声明此服务需要在后台长时运行。在onStop回调方法里,调用长时任务取消接口,声明取消长时任务。
在service.js文件中:
在service.js文件中:
```
js
import
backgroundTaskManager
from
'
@ohos.backgroundTaskManager
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
...
...
zh-cn/application-dev/background-task-management/background-task-overview.md
浏览文件 @
0d9c4a4e
...
...
@@ -59,4 +59,4 @@ OpenHarmony提供了九种后台模式,供需要在后台做长时任务的业
-
如果任务结束,应用应主动退出后台模式。若在后台运行期间,系统检测到应用并未使用对应后台模式的资源,则会被挂起(Suspend)。
-
避免不合理地申请后台长时任务,长时任务类型要与应用的业务类型匹配。如表1所示。如果执行的任务和申请的类型不匹配,也会被系统检测到并被挂起(Suspend)。
-
长时任务是为了真正在后台长时间执行某个任务,如果一个应用申请了长时任务,但在实际运行过程中,并未真正运行或执行此类任务时,也会被系统检测到并被挂起(Suspend)。
-
每个元能力同一时刻只能申请运行一个长时任务。
\ No newline at end of file
-
每个Ability同一时刻只能申请运行一个长时任务。
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录