文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
API参考应用框架Background Tasks Kit(后台任务开发服务)ArkTS API已停止维护的接口@ohos.backgroundTaskManager (后台任务管理)

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

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

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

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

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

说明

导入模块

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

backgroundTaskManager.requestSuspendDelay(deprecated)

PhonePC/2in1TabletTVWearable

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

后台应用申请延迟挂起。

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

说明

从API version 7开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.requestSuspendDelay替代。

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

参数

展开
参数名 类型 必填 说明
reason string 延迟挂起申请的原因。
callback Callback<void> 延迟即将超时的回调函数,一般在超时前6秒通过此回调通知应用。

返回值

展开
类型 说明
DelaySuspendInfo 返回延迟挂起信息。

示例

收起
自动换行
深色代码主题
复制
  1. import backgroundTaskManager from '@ohos.backgroundTaskManager';
  2. import { BusinessError } from '@ohos.base';
  3. // 设置延迟任务挂起的原因
  4. let myReason = 'test requestSuspendDelay';
  5. // 申请延迟任务
  6. let delayInfo = backgroundTaskManager.requestSuspendDelay(myReason, () => {
  7. console.info("Request suspension delay will time out.");
  8. })
  9. // 打印延迟任务信息
  10. let id = delayInfo.requestId;
  11. let time = delayInfo.actualDelayTime;
  12. console.info("The requestId is: " + id);
  13. console.info("The actualDelayTime is: " + time);

backgroundTaskManager.getRemainingDelayTime(deprecated)

PhonePC/2in1TabletTVWearable

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

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

说明

从API version 7开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.getRemainingDelayTime替代。

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

参数

展开
参数名 类型 必填 说明
requestId number 延迟挂起的请求ID。这个值通过调用requestSuspendDelay方法获取。
callback AsyncCallback<number> 指定的callback回调方法。用于返回应用程序进入挂起状态之前的剩余时间,单位:ms。

示例

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

backgroundTaskManager.getRemainingDelayTime(deprecated)

PhonePC/2in1TabletTVWearable

getRemainingDelayTime(requestId: number): Promise<number>

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

说明

从API version 7开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.getRemainingDelayTime替代。

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

参数

展开
参数名 类型 必填 说明
requestId number 延迟挂起的请求ID。这个值通过调用requestSuspendDelay方法获取。

返回值

展开
类型 说明
Promise<number> 指定的Promise回调方法。返回应用程序进入挂起状态之前的剩余时间,单位:ms。

示例

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

backgroundTaskManager.cancelSuspendDelay(deprecated)

PhonePC/2in1TabletTVWearable

cancelSuspendDelay(requestId: number): void

取消延迟挂起。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.cancelSuspendDelay替代。

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

参数

展开
参数名 类型 必填 说明
requestId number 延迟挂起的请求ID。这个值通过调用requestSuspendDelay方法获取。

示例

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

backgroundTaskManager.startBackgroundRunning(deprecated)

PhonePC/2in1TabletTVWearable

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

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

说明

从API version 8开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.startBackgroundRunning替代。

需要权限: 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, { WantAgent } from '@ohos.app.ability.wantAgent';
  4. import { BusinessError } from '@ohos.base';
  5. function callback(err: BusinessError, data: void) {
  6. if (err) {
  7. console.error("Operation startBackgroundRunning failed Cause: " + err);
  8. } else {
  9. console.info("Operation startBackgroundRunning succeeded");
  10. }
  11. }
  12. let wantAgentInfo : wantAgent.WantAgentInfo = {
  13. wants: [
  14. {
  15. bundleName: "com.example.myapplication",
  16. abilityName: "EntryAbility"
  17. }
  18. ],
  19. operationType: wantAgent.OperationType.START_ABILITY,
  20. requestCode: 0,
  21. wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
  22. };
  23. wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj : WantAgent) => {
  24. backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
  25. backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj, callback)
  26. });

Stage模型示例:

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

backgroundTaskManager.startBackgroundRunning(deprecated)

PhonePC/2in1TabletTVWearable

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

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

说明

从API version 8开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.startBackgroundRunning替代。

需要权限: 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模型示例(需使用js代码开发):

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

Stage模型示例:

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

backgroundTaskManager.stopBackgroundRunning(deprecated)

PhonePC/2in1TabletTVWearable

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

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

说明

从API version 8开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.stopBackgroundRunning替代。

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

参数

展开
参数名 类型 必填 说明
context Context

应用运行的上下文。

FA模型的应用Context定义见Context

Stage模型的应用Context定义见Context

callback AsyncCallback<void> callback形式返回启动长时任务的结果。

示例

FA模型示例(需使用js代码开发):

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

Stage模型示例:

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

backgroundTaskManager.stopBackgroundRunning(deprecated)

PhonePC/2in1TabletTVWearable

stopBackgroundRunning(context: Context): Promise<void>

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

说明

从API version 8开始支持,从API version 9开始废弃。建议使用backgroundTaskManager.stopBackgroundRunning替代。

系统能力: 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. import { BusinessError } from '@ohos.base';
  4. // 取消长时任务
  5. backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => {
  6. console.info("Operation stopBackgroundRunning succeeded");
  7. }).catch((err: BusinessError) => {
  8. console.error("Operation stopBackgroundRunning failed Cause: " + err);
  9. });

Stage模型示例:

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

DelaySuspendInfo(deprecated)

PhonePC/2in1TabletTVWearable

延迟挂起信息。

说明

从API version 7开始支持,从API version 9开始废弃。建议使用DelaySuspendInfo替代。

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

展开
名称 类型 只读 可选 说明
requestId number 延迟挂起的请求ID。
actualDelayTime number

应用的实际挂起延迟时间,单位:ms。

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

BackgroundMode(deprecated)

PhonePC/2in1TabletTVWearable

长时任务类型。

说明

从API version 8开始支持,从API version 9开始废弃。建议使用BackgroundMode替代。

系统能力: 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参考 中进行搜索
请输入您想要搜索的关键词