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