1# WorkSchedulerExtensionContext (System API)
2
3The **WorkSchedulerExtensionContext** module, inherited from [ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md), provides a context environment for the WorkSchedulerExtensionAbility.
4
5This module provides APIs for accessing the resources of a WorkSchedulerExtensionAbility.
6
7> **NOTE**
8>
9> 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.
10>
11> The APIs of this module can be used only in the stage model.
12>
13> The APIs provided by this module are system APIs.
14
15## How to Use
16
17The context is obtained through a WorkSchedulerExtensionAbility child class instance.
18
19```ts
20import { WorkSchedulerExtensionAbility, workScheduler } from from '@kit.BackgroundTasksKit';
21
22class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
23    onWorkStart(workInfo: workScheduler.WorkInfo) {
24        let WorkSchedulerExtensionContext = this.context; // Obtain the WorkSchedulerExtensionContext.
25    }
26}
27```
28
29## WorkSchedulerExtensionContext.startServiceExtensionAbility<sup>13+</sup>
30
31startServiceExtensionAbility(want: Want): Promise\<void>
32
33Starts a ServiceExtensionAbility. This API uses a promise to return the result.
34
35**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
36
37**Parameters**
38
39| Parameter| Type| Mandatory| Description|
40| -------- | -------- | -------- | -------- |
41| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
42
43**Returns**
44
45| Type| Description|
46| -------- | -------- |
47| Promise&lt;void&gt; | Promise that returns no value.|
48
49**Error codes**
50
51For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](../apis-ability-kit/errorcode-ability.md).
52
53| ID| Error Message|
54| ------- | -------- |
55| 201 | The application does not have permission to call the interface. |
56| 202 | The application is not system-app, can not use system-api. |
57| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
58| 16000001 | The specified ability does not exist. |
59| 16000002 | Incorrect ability type. |
60| 16000004 | Can not start invisible component. |
61| 16000005 | The specified process does not have the permission. |
62| 16000006 | Cross-user operations are not allowed. |
63| 16000008 | The crowdtesting application expires. |
64| 16000011 | The context does not exist.        |
65| 16000012 | The application is controlled.        |
66| 16000013 | The application is controlled by EDM.       |
67| 16000019 | Can not match any component. |
68| 16000050 | Internal error. |
69| 16200001 | The caller has been released. |
70
71**Example**
72
73```ts
74import { WorkSchedulerExtensionAbility, workScheduler } from '@kit.BackgroundTasksKit';
75import { Want } from '@kit.AbilityKit';
76import { BusinessError } from '@kit.BasicServicesKit';
77
78let want : Want = {
79  bundleName: 'com.example.workscheduler',
80  abilityName: 'ServiceExtAbility'
81}
82
83export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
84  onWorkStart(workInfo: workScheduler.WorkInfo) {
85    console.info(`onWorkStart, workInfo = ${JSON.stringify(workInfo)}`);
86      // Start the corresponding service.
87      this.context.startServiceExtensionAbility(want).then(() => {
88        console.info('succeeded in starting ServiceExtensionAbility.');
89      }).catch ((err: BusinessError) => {
90        console.error('failed to start ServiceExtensionAbility.');
91      });
92  }
93
94  onWorkStop(workInfo: workScheduler.WorkInfo) {
95    console.info(`onWorkStop, workInfo is ${JSON.stringify(workInfo)}`);
96  }
97}
98```
99
100## WorkSchedulerExtensionContext.stopServiceExtensionAbility<sup>13+</sup>
101
102stopServiceExtensionAbility(want: Want): Promise\<void>
103
104Stops a ServiceExtensionAbility. This API uses a promise to return the result.
105
106**System capability**: SystemCapability.ResourceSchedule.WorkScheduler
107
108**Parameters**
109
110| Parameter| Type| Mandatory| Description|
111| -------- | -------- | -------- | -------- |
112| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
113
114**Returns**
115
116| Type| Description|
117| -------- | -------- |
118| Promise&lt;void&gt; | Promise that returns no value.|
119
120**Error codes**
121
122For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](../apis-ability-kit/errorcode-ability.md).
123
124| ID| Error Message|
125| ------- | -------- |
126| 201 | The application does not have permission to call the interface. |
127| 202 | The application is not system-app, can not use system-api. |
128| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
129| 16000001 | The specified ability does not exist. |
130| 16000002 | Incorrect ability type. |
131| 16000004 | Can not start invisible component. |
132| 16000005 | The specified process does not have the permission. |
133| 16000006 | Cross-user operations are not allowed. |
134| 16000011 | The context does not exist.        |
135| 16000050 | Internal error. |
136| 16200001 | The caller has been released. |
137
138**Example**
139
140```ts
141import { WorkSchedulerExtensionAbility, workScheduler } from '@kit.BackgroundTasksKit';
142import { Want } from '@kit.AbilityKit';
143import { BusinessError } from '@kit.BasicServicesKit';
144
145let want : Want = {
146  bundleName: 'com.example.workscheduler',
147  abilityName: 'ServiceExtAbility'
148}
149
150export default class MyWorkSchedulerExtensionAbility extends WorkSchedulerExtensionAbility {
151  onWorkStart(workInfo: workScheduler.WorkInfo) {
152    console.info(`onWorkStart, workInfo = ${JSON.stringify(workInfo)}`);
153  }
154
155  onWorkStop(workInfo: workScheduler.WorkInfo) {
156    console.info(`onWorkStop, workInfo is ${JSON.stringify(workInfo)}`);
157      // Stop the corresponding service.
158      this.context.stopServiceExtensionAbility(want).then(() => {
159        console.info('succeeded in stopping ServiceExtensionAbility.');
160      }).catch ((err: BusinessError) => {
161        console.error('failed to stop ServiceExtensionAbility.');
162      });
163  }
164}
165```
166