• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# WindowExtensionContext
2
3The **WindowExtensionContext** module, inherited from [ExtensionContext](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.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 from '@ohos.application.WindowExtensionAbility';
21import WindowExtensionContext from 'application/WindowExtensionContext';
22
23let context: WindowExtensionContext | null = null;
24
25class WindowExtAbility extends WindowExtensionAbility {
26  onConnect() {
27    context = this.context; // Obtain a WindowExtensionContext instance.
28  }
29}
30```
31
32## WindowExtensionContext.startAbility
33
34startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void
35
36Starts an ability. This API uses an asynchronous callback to return the result.
37
38**System capability**: SystemCapability.WindowManager.WindowManager.Core
39
40**Parameters**
41
42| Name| Type| Mandatory| Description|
43| -------- | -------- | -------- | -------- |
44| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability.|
45| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.|
46| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
47
48**Example**
49
50  ```ts
51import { BusinessError } from '@ohos.base';
52import Want from '@ohos.app.ability.Want';
53import StartOptions from '@ohos.app.ability.StartOptions';
54
55let want: Want = {
56  bundleName: 'com.example.myapplication',
57  abilityName: 'MainAbility'
58};
59let options: StartOptions = {
60  windowMode: 102
61};
62
63try {
64  this.context.startAbility(want, options, (error: BusinessError) => {
65    let message = (error as BusinessError).message;
66    let errCode = (error as BusinessError).code;
67    if (errCode) {
68      // Process service logic errors.
69      console.error('startAbility failed, error.code: ${errCode}, error.message: ${message}');
70      return;
71    }
72    // Carry out normal service processing.
73    console.log('startAbility succeed');
74  });
75} catch (paramError) {
76  // Process input parameter errors.
77  let message = (paramError as BusinessError).message;
78  let errCode = (paramError as BusinessError).code;
79  console.error('error.code: ${errCode}, error.message: ${message}');
80}
81  ```
82
83## WindowExtensionContext.startAbility
84
85startAbility(want: Want, options?: StartOptions): Promise\<void>
86
87Starts an ability. This API uses a promise to return the result.
88
89**System capability**: SystemCapability.WindowManager.WindowManager.Core
90
91**Parameters**
92
93| Name| Type| Mandatory| Description|
94| -------- | -------- | -------- | -------- |
95| want | [Want](js-apis-app-ability-want.md)  | Yes| Want information about the target ability, such as the ability name and bundle name.|
96| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.|
97
98**Return value**
99
100| Type| Description|
101| -------- | -------- |
102| Promise&lt;void&gt; | Promise that returns no value.|
103
104**Example**
105
106  ```ts
107import { BusinessError } from '@ohos.base';
108import Want from '@ohos.app.ability.Want';
109import StartOptions from '@ohos.app.ability.StartOptions';
110
111let want: Want = {
112  bundleName: 'com.example.myapp',
113  abilityName: 'MainAbility'
114};
115let options: StartOptions = {
116  windowMode: 102,
117};
118
119try {
120  this.context.startAbility(want, options)
121    .then(() => {
122      // Carry out normal service processing.
123      console.log('startAbility succeed');
124    })
125    .catch((error: BusinessError) => {
126      // Process service logic errors.
127      let message = (error as BusinessError).message;
128      let errCode = (error as BusinessError).code;
129      console.error('startAbility failed, error.code: ${errCode}, error.message: ${message}');
130    });
131} catch (paramError) {
132  // Process input parameter errors.
133  let message = (paramError as BusinessError).message;
134  let errCode = (paramError as BusinessError).code;
135  console.error('error.code: ${errCode}, error.message: ${message}');
136}
137  ```
138