openharmony 鸿蒙 js-apis-resourceschedule-backgroundTaskManager-sys

2025-06-12 浏览 (1)

@ohos.resourceschedule.backgroundTaskManager (Background Task Management) (System API)

The backgroundTaskManager module provides APIs to request background tasks. You can use the APIs to request transient tasks, continuous tasks, or efficiency resources to prevent the application process from being terminated or suspended when your application is switched to the background.

NOTE

Modules to Import

import { backgroundTaskManager } from '@kit.BackgroundTasksKit';  

backgroundTaskManager.applyEfficiencyResources

applyEfficiencyResources(request: EfficiencyResourcesRequest): void

Requests efficiency resources.

System capability: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
requestEfficiencyResourcesRequestYesNecessary information carried in the request, including the resource type and timeout interval.

Error codes

For details about the error codes, see Universal Error Codes and backgroundTaskManager Error Codes.

IDError Message
201Permission denied.
202Not System App.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed.
9800001Memory operation failed.
9800002Parcel operation failed.
9800003Internal transaction failed.
9800004System service operation failed.
18700001Caller information verification failed for an energy resource request.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let request: backgroundTaskManager.EfficiencyResourcesRequest = {
    resourceTypes: backgroundTaskManager.ResourceType.CPU,
    isApply: true,
    timeOut: 0,
    reason: "apply",
    isPersist: true,
    isProcess: false,
};
try {
    backgroundTaskManager.applyEfficiencyResources(request);
    console.info("applyEfficiencyResources success. ");
} catch (error) {
    console.error(`applyEfficiencyResources failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
}

backgroundTaskManager.resetAllEfficiencyResources

resetAllEfficiencyResources(): void

Releases all efficiency resources.

System capability: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply

System API: This is a system API.

Error codes

For details about the error codes, see Universal Error Codes and backgroundTaskManager Error Codes.

IDError Message
201Permission denied.
202Not System App.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified.
9800001Memory operation failed.
9800002Parcel operation failed.
9800003Internal transaction failed.
9800004System service operation failed.
18700001Caller information verification failed for an energy resource request.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
    backgroundTaskManager.resetAllEfficiencyResources();
} catch (error) {
    console.error(`resetAllEfficiencyResources failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
}

BackgroundMode

Enumerates the continuous task modes.

System capability: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask

NameValueDescription
WIFI_INTERACTION7WLAN-related.
System API: This is a system API.

EfficiencyResourcesRequest

Describes the parameters for requesting efficiency resources.

System capability: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply

System API: This is a system API.

NameTypeMandatoryDescription
resourceTypesnumberYesType of the resource to request.
isApplybooleanYesWhether the request is used to apply for resources.
- true: The request is used to apply for resources.
- false: The request is used to release resources.
timeOutnumberYesDuration for which the resource will be used, in milliseconds.
isPersistbooleanNoWhether the resource is permanently held. The default value is false.
- true: The resource is permanently held.
- false: The resource is held for a limited period of time.
isProcessbooleanNoWhether the request is initiated by a process. The default value is false.
- true: The request is initiated by a process.
- false: The request is initiated by an application.
reasonstringYesReason for requesting the resource.

ResourceType

Enumerates the efficiency resource types.

System capability: SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply

System API: This is a system API.

NameValueDescription
CPU1CPU resource. Such type of resource prevents an application from being suspended.
COMMON_EVENT2Common event resource. Such type of resource ensures that an application in the suspended state can receive common events.
TIMER4Timer resource. Such type of resource ensures that an application in the suspended state can be woken up by system timers.
WORK_SCHEDULER8Deferred task resource. Such type of resource provides a loose control policy for an application.
BLUETOOTH16Bluetooth resource. Such type of resource ensures that an application in the suspended state can be woken up by Bluetooth-related events.
GPS32GPS resource. Such type of resource ensures that an application in the suspended state can be woken up by GPS-related events.
AUDIO64Audio resource. Such type of resource prevents an application from being suspended when the application has an audio being played.
RUNNING_LOCK10+128RUNNING_LOCK resources are not proxied when the application is suspended.
SENSOR10+256Sensor callbacks are not intercepted.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Background Tasks Kit

harmony 鸿蒙BackgroundProcessManager

harmony 鸿蒙TransientTask

harmony 鸿蒙TransientTask_DelaySuspendInfo

harmony 鸿蒙background_process_manager.h

harmony 鸿蒙DeviceUsageStatistics Error Codes

harmony 鸿蒙backgroundTaskManager Error Codes

harmony 鸿蒙reminderAgentManager Error Codes

harmony 鸿蒙workScheduler Error Codes

harmony 鸿蒙@ohos.WorkSchedulerExtensionAbility (Deferred Task Scheduling Callbacks)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/BSRFws