1e41f4b71Sopenharmony_ci# WindowExtensionContext (系统接口)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciWindowExtensionContext模块是WindowExtensionAbility的上下文环境,继承自[ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md)。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciWindowExtensionContext模块提供[WindowExtensionAbility](js-apis-application-windowExtensionAbility-sys.md)具有的能力,包括启动Ability。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **说明:**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci>  - 本模块接口为系统接口。
12e41f4b71Sopenharmony_ci>
13e41f4b71Sopenharmony_ci>  - 本模块接口仅可在Stage模型下使用。
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## 使用说明
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci在使用WindowExtensionContext的功能前,需要通过WindowExtensionAbility子类实例获取。
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci```ts
20e41f4b71Sopenharmony_ciimport { WindowExtensionAbility, WindowExtensionContext } from '@kit.ArkUI';
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_cilet context: WindowExtensionContext | null = null;
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciclass WindowExtAbility extends WindowExtensionAbility {
25e41f4b71Sopenharmony_ci  onConnect() {
26e41f4b71Sopenharmony_ci    context = this.context; // 获取WindowExtensionContext
27e41f4b71Sopenharmony_ci  }
28e41f4b71Sopenharmony_ci}
29e41f4b71Sopenharmony_ci```
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci## WindowExtensionContext.startAbility
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_cistartAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci启动Ability,使用callback异步回调。
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.WindowManager.WindowManager.Core
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**参数:**
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
42e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
43e41f4b71Sopenharmony_ci| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md)  | 是 | 启动Ability的want信息。 |
44e41f4b71Sopenharmony_ci| options | [StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
45e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 |
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**错误码:**
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
52e41f4b71Sopenharmony_ci| ------- | --------------------------------------------- |
53e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API. |
54e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible cause: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci**示例:**
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci```ts
59e41f4b71Sopenharmony_ciimport { WindowExtensionAbility } from '@kit.ArkUI';
60e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
61e41f4b71Sopenharmony_ciimport { Want, StartOptions } from '@kit.AbilityKit';
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ciclass WindowExtAbility extends WindowExtensionAbility {
64e41f4b71Sopenharmony_ci  
65e41f4b71Sopenharmony_ci  onConnect() {
66e41f4b71Sopenharmony_ci    let want: Want = {
67e41f4b71Sopenharmony_ci      bundleName: 'com.example.myapplication',
68e41f4b71Sopenharmony_ci      abilityName: 'MainAbility'
69e41f4b71Sopenharmony_ci    };
70e41f4b71Sopenharmony_ci    let options: StartOptions = {
71e41f4b71Sopenharmony_ci      windowMode: 102
72e41f4b71Sopenharmony_ci    };
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci    try {
75e41f4b71Sopenharmony_ci      this.context.startAbility(want, options, (error: BusinessError) => {
76e41f4b71Sopenharmony_ci        let message = (error as BusinessError).message;
77e41f4b71Sopenharmony_ci        let errCode = (error as BusinessError).code;
78e41f4b71Sopenharmony_ci        if (errCode) {
79e41f4b71Sopenharmony_ci          // 处理业务逻辑错误
80e41f4b71Sopenharmony_ci          console.error(`startAbility failed, error.code: ${errCode}, error.message: ${message}`);
81e41f4b71Sopenharmony_ci          return;
82e41f4b71Sopenharmony_ci        }
83e41f4b71Sopenharmony_ci        // 执行正常业务
84e41f4b71Sopenharmony_ci        console.log('startAbility succeed');
85e41f4b71Sopenharmony_ci      });
86e41f4b71Sopenharmony_ci    } catch (paramError) {
87e41f4b71Sopenharmony_ci      // 处理入参错误异常
88e41f4b71Sopenharmony_ci      let message = (paramError as BusinessError).message;
89e41f4b71Sopenharmony_ci      let errCode = (paramError as BusinessError).code;
90e41f4b71Sopenharmony_ci      console.error(`error.code: ${errCode}, error.message: ${message}`);
91e41f4b71Sopenharmony_ci    }
92e41f4b71Sopenharmony_ci  }
93e41f4b71Sopenharmony_ci}
94e41f4b71Sopenharmony_ci```
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci## WindowExtensionContext.startAbility
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_cistartAbility(want: Want, options?: StartOptions): Promise\<void>
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci启动Ability,使用Promise异步回调。
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.WindowManager.WindowManager.Core
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci**参数:**
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 |
107e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
108e41f4b71Sopenharmony_ci| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
109e41f4b71Sopenharmony_ci| options | [StartOptions](../apis-ability-kit/js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci**返回值:**
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci| 类型 | 说明 |
114e41f4b71Sopenharmony_ci| -------- | -------- |
115e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci**错误码:**
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
122e41f4b71Sopenharmony_ci| ------- | --------------------------------------------- |
123e41f4b71Sopenharmony_ci| 202     | Permission verification failed. A non-system application calls a system API. |
124e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible cause: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci**示例:**
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci```ts
129e41f4b71Sopenharmony_ciimport { WindowExtensionAbility } from '@kit.ArkUI';
130e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
131e41f4b71Sopenharmony_ciimport { Want, StartOptions } from '@kit.AbilityKit';
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ciclass WindowExtAbility extends WindowExtensionAbility {
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci  onConnect() {
136e41f4b71Sopenharmony_ci    let want: Want = {
137e41f4b71Sopenharmony_ci      bundleName: 'com.example.myapp',
138e41f4b71Sopenharmony_ci      abilityName: 'MainAbility'
139e41f4b71Sopenharmony_ci    };
140e41f4b71Sopenharmony_ci    let options: StartOptions = {
141e41f4b71Sopenharmony_ci      windowMode: 102,
142e41f4b71Sopenharmony_ci    };
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci    try {
145e41f4b71Sopenharmony_ci      this.context.startAbility(want, options)
146e41f4b71Sopenharmony_ci        .then(() => {
147e41f4b71Sopenharmony_ci          // 执行正常业务
148e41f4b71Sopenharmony_ci          console.log('startAbility succeed');
149e41f4b71Sopenharmony_ci        })
150e41f4b71Sopenharmony_ci        .catch((error: BusinessError) => {
151e41f4b71Sopenharmony_ci          // 处理业务逻辑错误
152e41f4b71Sopenharmony_ci          let message = (error as BusinessError).message;
153e41f4b71Sopenharmony_ci          let errCode = (error as BusinessError).code;
154e41f4b71Sopenharmony_ci          console.error(`startAbility failed, error.code: ${errCode}, error.message: ${message}`);
155e41f4b71Sopenharmony_ci        });
156e41f4b71Sopenharmony_ci    } catch (paramError) {
157e41f4b71Sopenharmony_ci      // 处理入参错误异常
158e41f4b71Sopenharmony_ci      let message = (paramError as BusinessError).message;
159e41f4b71Sopenharmony_ci      let errCode = (paramError as BusinessError).code;
160e41f4b71Sopenharmony_ci      console.error(`error.code: ${errCode}, error.message: ${message}`);
161e41f4b71Sopenharmony_ci    }
162e41f4b71Sopenharmony_ci  }
163e41f4b71Sopenharmony_ci}
164e41f4b71Sopenharmony_ci```