文档管理中心

您当前浏览的3.1/4.0版本文档归档不再维护,推荐您使用最新的HarmonyOS NEXT版本文档。详细请参考文档维护策略变更

API参考ArkTS API参考ArkTS接口参考已停止维护的接口@ohos.backgroundTaskManager (后台任务管理)

@ohos.backgroundTaskManager (后台任务管理)

本模块提供后台任务管理能力。

当应用或业务模块处于后台(无可见界面)时,如果有需要继续执行或者后续执行的业务,可基于业务类型,申请短时任务延迟挂起(Suspend)或者长时任务避免进入挂起状态。

应用有不可中断且短时间能完成的任务时(如,用户在文件管理器上点击垃圾文件清理,若清理未完成时退到后台,文件管理器需要申请短时任务完成清理),可以使用短时任务机制。

应用中存在用户能够直观感受到的且需要一直在后台运行的业务时(如,后台播放音乐),可以使用长时任务机制。

对于系统特权应用,提供独立的能效资源申请接口。系统特权应用如果需要使用特定的系统资源,例如需要在被挂起期间仍然能够收到系统公共事件,可以使用能效资源申请接口。

说明

导入模块

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';

backgroundTaskManager.requestSuspendDelay

requestSuspendDelay(reason: string, callback: Callback<void>): DelaySuspendInfo

后台应用申请延迟挂起。

延迟挂起时间一般情况下默认值为3分钟,低电量(依据系统低电量广播)时默认值为1分钟。

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.TransientTask

参数

展开

参数名

类型

必填

说明

reason

string

延迟挂起申请的原因。

callback

Callback<void>

延迟即将超时的回调函数,一般在超时前6秒通过此回调通知应用。

返回值

展开

类型

说明

DelaySuspendInfo

返回延迟挂起信息。

示例

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  2. let myReason = 'test requestSuspendDelay';
  3. let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => {
  4. console.info("Request suspension delay will time out.");
  5. })
  6. let id = delayInfo.requestId;
  7. let time = delayInfo.actualDelayTime;
  8. console.info("The requestId is: " + id);
  9. console.info("The actualDelayTime is: " + time);

backgroundTaskManager.getRemainingDelayTime

getRemainingDelayTime(requestId: number, callback: AsyncCallback<number>): void

获取应用程序进入挂起状态前的剩余时间,使用callback形式返回。

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.TransientTask

参数

展开

参数名

类型

必填

说明

requestId

number

延迟挂起的请求ID。这个值通过调用requestSuspendDelay方法获取。

callback

AsyncCallback<number>

指定的callback回调方法。用于返回应用程序进入挂起状态之前的剩余时间,以毫秒为单位。

示例

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  2. let delayInfo = backgroundTaskManager.requestSuspendDelay("test", () => {});
  3. backgroundTaskManager.getRemainingDelayTime(delayInfo.requestId, (err, res) => {
  4. if(err) {
  5. console.log('callback => Operation getRemainingDelayTime failed. Cause: ' + err.code);
  6. } else {
  7. console.log('callback => Operation getRemainingDelayTime succeeded. Data: ' + JSON.stringify(res));
  8. }
  9. })

backgroundTaskManager.getRemainingDelayTime

getRemainingDelayTime(requestId: number): Promise<number>

获取应用程序进入挂起状态前的剩余时间,使用Promise形式返回。

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.TransientTask

参数

展开

参数名

类型

必填

说明

requestId

number

延迟挂起的请求ID。这个值通过调用requestSuspendDelay方法获取。

返回值

展开

类型

说明

Promise<number>

指定的Promise回调方法。返回应用程序进入挂起状态之前的剩余时间,以毫秒为单位。

示例

收起
自动换行
深色代码主题
复制
  1. let delayInfo = backgroundTaskManager.requestSuspendDelay("test", () => {});
  2. backgroundTaskManager.getRemainingDelayTime(delayInfo.requestId).then( res => {
  3. console.log('promise => Operation getRemainingDelayTime succeeded. Data: ' + JSON.stringify(res));
  4. }).catch( err => {
  5. console.log('promise => Operation getRemainingDelayTime failed. Cause: ' + err.code);
  6. })

backgroundTaskManager.cancelSuspendDelay

cancelSuspendDelay(requestId: number): void

取消延迟挂起。

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.TransientTask

参数

展开

参数名

类型

必填

说明

requestId

number

延迟挂起的请求ID。这个值通过调用requestSuspendDelay方法获取。

示例

收起
自动换行
深色代码主题
复制
  1. let delayInfo = backgroundTaskManager.requestSuspendDelay("test", () => {});
  2. backgroundTaskManager.cancelSuspendDelay(delayInfo.requestId);

backgroundTaskManager.startBackgroundRunning8+

startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: WantAgent, callback: AsyncCallback<void>): void

向系统申请长时任务,使用callback形式返回结果。

需要权限: ohos.permission.KEEP_BACKGROUND_RUNNING

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask

参数

展开

参数名

类型

必填

说明

context

Context

应用运行的上下文。

FA模型的应用Context定义见Context

Stage模型的应用Context定义见Context

bgMode

BackgroundMode

向系统申请的后台模式。

wantAgent

WantAgent

通知参数,用于指定长时任务通知点击后跳转的界面。

callback

AsyncCallback<void>

callback形式返回启动长时任务的结果。

示例

FA模型示例:

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  2. import featureAbility from '@ohos.ability.featureAbility';
  3. import wantAgent from '@ohos.wantAgent';
  4. function callback(err, data) {
  5. if (err) {
  6. console.error("Operation startBackgroundRunning failed Cause: " + err);
  7. } else {
  8. console.info("Operation startBackgroundRunning succeeded");
  9. }
  10. }
  11. let wantAgentInfo = {
  12. wants: [
  13. {
  14. bundleName: "com.example.myapplication",
  15. abilityName: "com.example.myapplication.MainAbility"
  16. }
  17. ],
  18. operationType: wantAgent.OperationType.START_ABILITY,
  19. requestCode: 0,
  20. wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  21. };
  22. wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
  23. backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
  24. backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj, callback)
  25. });

Stage模型示例:

收起
自动换行
深色代码主题
复制
  1. import UIAbility from '@ohos.app.ability.UIAbility';
  2. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  3. import wantAgent from '@ohos.app.ability.wantAgent';
  4. function callback(err, data) {
  5. if (err) {
  6. console.error("Operation startBackgroundRunning failed Cause: " + err);
  7. } else {
  8. console.info("Operation startBackgroundRunning succeeded");
  9. }
  10. }
  11. export default class EntryAbility extends UIAbility {
  12. onCreate(want, launchParam) {
  13. let wantAgentInfo = {
  14. wants: [
  15. {
  16. bundleName: "com.example.myapplication",
  17. abilityName: "EntryAbility"
  18. }
  19. ],
  20. operationType: wantAgent.OperationType.START_ABILITY,
  21. requestCode: 0,
  22. wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  23. };
  24. wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
  25. backgroundTaskManager.startBackgroundRunning(this.context,
  26. backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj, callback)
  27. });
  28. }
  29. };

backgroundTaskManager.startBackgroundRunning8+

startBackgroundRunning(context: Context, bgMode: BackgroundMode, wantAgent: WantAgent): Promise<void>

向系统申请长时任务,使用promise形式返回结果。

需要权限: ohos.permission.KEEP_BACKGROUND_RUNNING

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask

参数

展开

参数名

类型

必填

说明

context

Context

应用运行的上下文。

FA模型的应用Context定义见Context

Stage模型的应用Context定义见Context

bgMode

BackgroundMode

向系统申请的后台模式。

wantAgent

WantAgent

通知参数,用于指定长时任务通知点击跳转的界面。

返回值

展开

类型

说明

Promise<void>

使用Promise形式返回结果。

示例

FA模型示例:

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  2. import featureAbility from '@ohos.ability.featureAbility';
  3. import wantAgent from '@ohos.wantAgent';
  4. let wantAgentInfo = {
  5. wants: [
  6. {
  7. bundleName: "com.example.myapplication",
  8. abilityName: "com.example.myapplication.MainAbility"
  9. }
  10. ],
  11. operationType: wantAgent.OperationType.START_ABILITY,
  12. requestCode: 0,
  13. wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  14. };
  15. wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
  16. backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
  17. backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => {
  18. console.info("Operation startBackgroundRunning succeeded");
  19. }).catch((err) => {
  20. console.error("Operation startBackgroundRunning failed Cause: " + err);
  21. });
  22. });

Stage模型示例:

收起
自动换行
深色代码主题
复制
  1. import UIAbility from '@ohos.app.ability.UIAbility';
  2. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  3. import wantAgent from '@ohos.app.ability.wantAgent';
  4. export default class EntryAbility extends UIAbility {
  5. onCreate(want, launchParam) {
  6. let wantAgentInfo = {
  7. wants: [
  8. {
  9. bundleName: "com.example.myapplication",
  10. abilityName: "EntryAbility"
  11. }
  12. ],
  13. operationType: wantAgent.OperationType.START_ABILITY,
  14. requestCode: 0,
  15. wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  16. };
  17. wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
  18. backgroundTaskManager.startBackgroundRunning(this.context,
  19. backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => {
  20. console.info("Operation startBackgroundRunning succeeded");
  21. }).catch((err) => {
  22. console.error("Operation startBackgroundRunning failed Cause: " + err);
  23. });
  24. });
  25. }
  26. };

backgroundTaskManager.stopBackgroundRunning8+

stopBackgroundRunning(context: Context, callback: AsyncCallback<void>): void

向系统申请取消长时任务,使用callback形式返回结果。

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask

参数

展开

参数名

类型

必填

说明

context

Context

应用运行的上下文。

FA模型的应用Context定义见Context

Stage模型的应用Context定义见Context

callback

AsyncCallback<void>

callback形式返回启动长时任务的结果。

示例

FA模型示例:

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  2. import featureAbility from '@ohos.ability.featureAbility';
  3. function callback(err, data) {
  4. if (err) {
  5. console.error("Operation stopBackgroundRunning failed Cause: " + err);
  6. } else {
  7. console.info("Operation stopBackgroundRunning succeeded");
  8. }
  9. }
  10. backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext(), callback);

Stage模型示例:

收起
自动换行
深色代码主题
复制
  1. import UIAbility from '@ohos.app.ability.UIAbility';
  2. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  3. function callback(err, data) {
  4. if (err) {
  5. console.error("Operation stopBackgroundRunning failed Cause: " + err);
  6. } else {
  7. console.info("Operation stopBackgroundRunning succeeded");
  8. }
  9. }
  10. export default class EntryAbility extends UIAbility {
  11. onCreate(want, launchParam) {
  12. backgroundTaskManager.stopBackgroundRunning(this.context, callback);
  13. }
  14. };

backgroundTaskManager.stopBackgroundRunning8+

stopBackgroundRunning(context: Context): Promise<void>

向系统申请取消长时任务,使用promise形式返回结果。

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask

参数

展开

参数名

类型

必填

说明

context

Context

应用运行的上下文。

FA模型的应用Context定义见Context

Stage模型的应用Context定义见Context

返回值

展开

类型

说明

Promise<void>

使用Promise形式返回结果。

示例

FA模型示例:

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  2. import featureAbility from '@ohos.ability.featureAbility';
  3. backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => {
  4. console.info("Operation stopBackgroundRunning succeeded");
  5. }).catch((err) => {
  6. console.error("Operation stopBackgroundRunning failed Cause: " + err);
  7. });

Stage模型示例:

收起
自动换行
深色代码主题
复制
  1. import UIAbility from '@ohos.app.ability.UIAbility';
  2. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  3. export default class EntryAbility extends UIAbility {
  4. onCreate(want, launchParam) {
  5. backgroundTaskManager.stopBackgroundRunning(this.context).then(() => {
  6. console.info("Operation stopBackgroundRunning succeeded");
  7. }).catch((err) => {
  8. console.error("Operation stopBackgroundRunning failed Cause: " + err);
  9. });
  10. }
  11. };

DelaySuspendInfo

延迟挂起信息。

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.TransientTask

展开

名称

类型

必填

说明

requestId

number

延迟挂起的请求ID。

actualDelayTime

number

应用的实际挂起延迟时间,以毫秒为单位。

一般情况下默认值为180000,低电量(依据系统低电量广播)时默认值为60000。

BackgroundMode8+

系统能力: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask

展开

名称

说明

DATA_TRANSFER

1

数据传输。

AUDIO_PLAYBACK

2

音频播放。

AUDIO_RECORDING

3

录音。

LOCATION

4

定位导航。

BLUETOOTH_INTERACTION

5

蓝牙相关。

MULTI_DEVICE_CONNECTION

6

多设备互联。

TASK_KEEPING

9

计算任务(仅在特定设备生效)。

在 API参考 中进行搜索
请输入您想要搜索的关键词