openharmony 鸿蒙 js-apis-WorkSchedulerExtensionContext-sys

2026-08-25 浏览 (1)

WorkSchedulerExtensionContext (System API)

The WorkSchedulerExtensionContext module, inherited from ExtensionContext, provides a context environment for the WorkSchedulerExtensionAbility.

This module directly serves as the context for WorkSchedulerExtension and provides the capability to access resources specific to WorkSchedulerExtensionAbility.

NOTE

The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs of this module can be used only in the stage model.

The APIs provided by this module are system APIs.

How to Use

Obtain the context through a WorkSchedulerExtensionAbility child class instance.

import { WorkSchedulerExtensionAbility, workScheduler } from '@kit.BackgroundTasksKit';

class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
    onWorkStart(workInfo: workScheduler.WorkInfo) {
        let WorkSchedulerExtensionContext = this.context; // Obtain the WorkSchedulerExtensionContext.
    }
}

WorkSchedulerExtensionContext.startServiceExtensionAbility13+

startServiceExtensionAbility(want: Want): Promise<void>

Starts a ServiceExtensionAbility. This API uses a promise to return the result.

System capability: SystemCapability.ResourceSchedule.WorkScheduler

Parameters

NameTypeMandatoryDescription
wantWantYesInformation about the Want used for starting an ability.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
201The application does not have permission to call the interface.
202The application is not system-app, can not use system-api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001The specified ability does not exist.
16000002Incorrect ability type.
16000004Cannot start an invisible component.
16000005The specified process does not have the permission.
16000006Cross-user operations are not allowed.
16000008The crowdtesting application expires.
16000011The context does not exist.
16000012The application is controlled.
16000013The application is controlled by EDM.
16000050Internal error.
16200001The caller has been released.

Example

import { WorkSchedulerExtensionAbility, workScheduler } from '@kit.BackgroundTasksKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let want : Want = {
  bundleName: 'com.example.workscheduler',
  abilityName: 'ServiceExtAbility'
}

export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
  onWorkStart(workInfo: workScheduler.WorkInfo) {
    console.info(`onWorkStart, workInfo = ${JSON.stringify(workInfo)}`);
      // Start the corresponding service.
      this.context.startServiceExtensionAbility(want).then(() => {
        console.info('succeeded in starting ServiceExtensionAbility.');
      }).catch ((err: BusinessError) => {
        console.error('failed to start ServiceExtensionAbility.');
      });
  }

  onWorkStop(workInfo: workScheduler.WorkInfo) {
    console.info(`onWorkStop, workInfo is ${JSON.stringify(workInfo)}`);
  }
}

WorkSchedulerExtensionContext.stopServiceExtensionAbility13+

stopServiceExtensionAbility(want: Want): Promise<void>

Stops a ServiceExtensionAbility. This API uses a promise to return the result.

System capability: SystemCapability.ResourceSchedule.WorkScheduler

Parameters

NameTypeMandatoryDescription
wantWantYesInformation about the Want used for stopping an ability.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
201The application does not have permission to call the interface.
202The application is not system-app, can not use system-api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001The specified ability does not exist.
16000002Incorrect ability type.
16000004Cannot start an invisible component.
16000005The specified process does not have the permission.
16000006Cross-user operations are not allowed.
16000011The context does not exist.
16000050Internal error.
16200001The caller has been released.

Example

import { WorkSchedulerExtensionAbility, workScheduler } from '@kit.BackgroundTasksKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let want : Want = {
  bundleName: 'com.example.workscheduler',
  abilityName: 'ServiceExtAbility'
}

export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
  onWorkStart(workInfo: workScheduler.WorkInfo) {
    console.info(`onWorkStart, workInfo = ${JSON.stringify(workInfo)}`);
  }

  onWorkStop(workInfo: workScheduler.WorkInfo) {
    console.info(`onWorkStop, workInfo is ${JSON.stringify(workInfo)}`);
      // Stop the corresponding service.
      this.context.stopServiceExtensionAbility(want).then(() => {
        console.info('succeeded in stopping ServiceExtensionAbility.');
      }).catch ((err: BusinessError) => {
        console.error('failed to stop ServiceExtensionAbility.');
      });
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-backgroundTaskManager

openharmony 鸿蒙 errorcode-DeviceUsageStatistics

openharmony 鸿蒙 capi-transient-task-type-h

openharmony 鸿蒙 errorcode-backgroundTaskMgr

openharmony 鸿蒙 js-apis-WorkSchedulerExtensionContext

openharmony 鸿蒙 js-apis-reminderAgent

openharmony 鸿蒙 errorcode-backgroundProcessManager

openharmony 鸿蒙 js-apis-WorkSchedulerExtensionAbility

openharmony 鸿蒙 errorcode-reminderAgentManager

openharmony 鸿蒙 capi-transienttask-transienttask-transienttaskinfo

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