未验证 提交 8693b294 编写于 作者: O openharmony_ci 提交者: Gitee

!11645 长时任务文档修改

Merge pull request !11645 from 朱天怡/master
......@@ -255,8 +255,8 @@ startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: Want
**示例**
```js
import Ability from '@ohos.application.Ability'
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility';
import wantAgent from '@ohos.wantAgent';
function callback(error, data) {
......@@ -267,27 +267,30 @@ function callback(error, data) {
}
}
let wantAgentInfo = {
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
let wantAgentInfo = {
wants: [
{
bundleName: "com.example.myapplication",
abilityName: "com.example.myapplication.MainAbility"
abilityName: "MainAbility"
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
try {
backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj, callback)
backgroundTaskManager.startBackgroundRunning(this.context,
backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj, callback)
} catch (error) {
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}
});
});
}
};
```
## backgroundTaskManager.startBackgroundRunning:promise
......@@ -331,26 +334,28 @@ startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: Want
**示例**
```js
import Ability from '@ohos.application.Ability'
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility';
import wantAgent from '@ohos.wantAgent';
let wantAgentInfo = {
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
let wantAgentInfo = {
wants: [
{
bundleName: "com.example.myapplication",
abilityName: "com.example.myapplication.MainAbility"
abilityName: "MainAbility"
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
try {
backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj).then(() => {
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}`);
......@@ -358,8 +363,9 @@ wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
} catch (error) {
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}
});
});
}
};
```
## backgroundTaskManager.stopBackgroundRunning:callback
......@@ -394,8 +400,8 @@ stopBackgroundRunning(context: Context, callback: AsyncCallback<void>): vo
**示例**
```js
import Ability from '@ohos.application.Ability'
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility';
function callback(error, data) {
if (error) {
......@@ -405,12 +411,15 @@ function callback(error, data) {
}
}
try {
backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext(), callback);
} catch (error) {
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
try {
backgroundTaskManager.stopBackgroundRunning(this.context, callback);
} catch (error) {
console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}
}
}
};
```
## backgroundTaskManager.stopBackgroundRunning:promise
......@@ -452,19 +461,22 @@ stopBackgroundRunning(context: Context): Promise<void>
**示例**
```js
import Ability from '@ohos.application.Ability'
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility';
try {
backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => {
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
try {
backgroundTaskManager.stopBackgroundRunning(this.context).then(() => {
console.info("Operation stopBackgroundRunning succeeded");
}).catch((error) => {
}).catch((err) => {
console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
});
} catch (error) {
} catch (error) {
console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}
}
}
};
```
## backgroundTaskManager.applyEfficiencyResources
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册