1# WorkSchedulerExtensionContext (系统接口)
2
3WorkSchedulerExtensionContext是WorkSchedulerExtensionAbility的上下文环境,继承自[ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md)。
4
5WorkSchedulerExtensionContext可直接作为WorkSchedulerExtension的上下文环境,提供允许访问特定于WorkSchedulerExtensionAbility的资源的能力。
6
7> **说明:**
8>
9> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10>
11> 本模块接口仅可在Stage模型下使用。
12>
13> 本模块为系统接口。
14
15## 使用说明
16
17通过WorkSchedulerExtensionAbility子类实例来获取。
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; // 获取WorkSchedulerExtensionContext
25    }
26}
27```
28
29## WorkSchedulerExtensionContext.startServiceExtensionAbility<sup>13+</sup>
30
31startServiceExtensionAbility(want: Want): Promise\<void>
32
33启动ServiceExtensionAbility,使用Promise异步回调。
34
35**系统能力**:SystemCapability.ResourceSchedule.WorkScheduler
36
37**参数:**
38
39| 参数名 | 类型 | 必填 | 说明 |
40| -------- | -------- | -------- | -------- |
41| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
42
43**返回值:**
44
45| 类型 | 说明 |
46| -------- | -------- |
47| Promise&lt;void&gt; | 无返回结果的Promise对象。|
48
49**错误码:**
50
51以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](../apis-ability-kit/errorcode-ability.md)。
52
53| 错误码ID | 错误信息 |
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**示例:**
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      // 拉起对应的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
104停止ServiceExtensionAbility,使用Promise异步回调。
105
106**系统能力**:SystemCapability.ResourceSchedule.WorkScheduler
107
108**参数:**
109
110| 参数名 | 类型 | 必填 | 说明 |
111| -------- | -------- | -------- | -------- |
112| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 |
113
114**返回值:**
115
116| 类型 | 说明 |
117| -------- | -------- |
118| Promise&lt;void&gt; | 无返回结果的Promise对象。|
119
120**错误码:**
121
122以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](../apis-ability-kit/errorcode-ability.md)。
123
124| 错误码ID | 错误信息 |
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**示例:**
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      // 停止对应的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```