1# WindowExtensionContext (System API)
2
3The **WindowExtensionContext** module, inherited from [ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md), is the context environment of the WindowExtensionAbility.
4
5The **WindowExtensionContext** module provides the capabilities of the [WindowExtensionAbility](js-apis-application-windowExtensionAbility-sys.md), including starting the ability.
6
7> **NOTE**
8>
9>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10>
11>  - The APIs provided by this module are system APIs.
12>
13>  - The APIs of this module can be used only in the stage model.
14
15## Usage
16
17Before using the **WindowExtensionContext** module, you must define a child class that inherits from **WindowExtensionAbility**.
18
19```ts
20import { WindowExtensionAbility, WindowExtensionContext } from '@kit.ArkUI';
21
22let context: WindowExtensionContext | null = null;
23
24class WindowExtAbility extends WindowExtensionAbility {
25  onConnect() {
26    context = this.context; // Obtain a WindowExtensionContext instance.
27  }
28}
29```
30
31## WindowExtensionContext.startAbility
32
33startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void
34
35Starts an ability. This API uses an asynchronous callback to return the result.
36
37**System capability**: SystemCapability.WindowManager.WindowManager.Core
38
39**Parameters**
40
41| Name | Type | Mandatory | Description |
42| -------- | -------- | -------- | -------- |
43| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md)  | Yes | Want information about the target ability. |
44| options | [StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md) | Yes | Parameters used for starting the ability. |
45| callback | AsyncCallback<void> | Yes | Callback used to return the result. |
46
47**Error codes**
48
49For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
50
51| ID | Error Message |
52| ------- | --------------------------------------------- |
53| 202     | Permission verification failed. A non-system application calls a system API. |
54| 401     | Parameter error. Possible cause: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
55
56**Example**
57
58```ts
59import { WindowExtensionAbility } from '@kit.ArkUI';
60import { BusinessError } from '@kit.BasicServicesKit';
61import { Want, StartOptions } from '@kit.AbilityKit';
62
63class WindowExtAbility extends WindowExtensionAbility {
64
65  onConnect() {
66    let want: Want = {
67      bundleName: 'com.example.myapplication',
68      abilityName: 'MainAbility'
69    };
70    let options: StartOptions = {
71      windowMode: 102
72    };
73
74    try {
75      this.context.startAbility(want, options, (error: BusinessError) => {
76        let message = (error as BusinessError).message;
77        let errCode = (error as BusinessError).code;
78        if (errCode) {
79          // Process service logic errors.
80          console.error(`startAbility failed, error.code: ${errCode}, error.message: ${message}`);
81          return;
82        }
83        // Carry out normal service processing.
84        console.log('startAbility succeed');
85      });
86    } catch (paramError) {
87      // Process input parameter errors.
88      let message = (paramError as BusinessError).message;
89      let errCode = (paramError as BusinessError).code;
90      console.error(`error.code: ${errCode}, error.message: ${message}`);
91    }
92  }
93}
94```
95
96## WindowExtensionContext.startAbility
97
98startAbility(want: Want, options?: StartOptions): Promise\<void>
99
100Starts an ability. This API uses a promise to return the result.
101
102**System capability**: SystemCapability.WindowManager.WindowManager.Core
103
104**Parameters**
105
106| Name | Type | Mandatory | Description |
107| -------- | -------- | -------- | -------- |
108| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md)  | Yes | Want information about the target ability, such as the ability name and bundle name. |
109| options | [StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md) | No | Parameters used for starting the ability. |
110
111**Return value**
112
113| Type | Description |
114| -------- | -------- |
115| Promise&lt;void&gt; | Promise that returns no value. |
116
117**Error codes**
118
119For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
120
121| ID | Error Message |
122| ------- | --------------------------------------------- |
123| 202     | Permission verification failed. A non-system application calls a system API. |
124| 401     | Parameter error. Possible cause: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
125
126**Example**
127
128```ts
129import { WindowExtensionAbility } from '@kit.ArkUI';
130import { BusinessError } from '@kit.BasicServicesKit';
131import { Want, StartOptions } from '@kit.AbilityKit';
132
133class WindowExtAbility extends WindowExtensionAbility {
134
135  onConnect() {
136    let want: Want = {
137      bundleName: 'com.example.myapp',
138      abilityName: 'MainAbility'
139    };
140    let options: StartOptions = {
141      windowMode: 102,
142    };
143
144    try {
145      this.context.startAbility(want, options)
146        .then(() => {
147          // Carry out normal service processing.
148          console.log('startAbility succeed');
149        })
150        .catch((error: BusinessError) => {
151          // Process service logic errors.
152          let message = (error as BusinessError).message;
153          let errCode = (error as BusinessError).code;
154          console.error(`startAbility failed, error.code: ${errCode}, error.message: ${message}`);
155        });
156    } catch (paramError) {
157      // Process input parameter errors.
158      let message = (paramError as BusinessError).message;
159      let errCode = (paramError as BusinessError).code;
160      console.error(`error.code: ${errCode}, error.message: ${message}`);
161    }
162  }
163}
164```
165