• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.InputMethodExtensionContext (InputMethodExtensionContext)
2<!--Kit: IME Kit-->
3<!--Subsystem: MiscServices-->
4<!--Owner: @illybyy-->
5<!--Designer: @andeszhang-->
6<!--Tester: @murphy1984-->
7<!--Adviser: @zhang_yixin13-->
8
9The **InputMethodExtensionContext** module, inherited from **ExtensionContext**, provides context for **InputMethodExtension** abilities. You can use the APIs of this module to start, terminate, connect, and disconnect abilities.
10
11> **NOTE**
12>
13> 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.
14> The APIs of this module can be used only in the stage model.
15
16## Modules to Import
17
18```ts
19import { InputMethodExtensionContext } from '@kit.IMEKit';
20```
21
22## Usage
23
24Before using the **InputMethodExtensionContext** module, you must define a child class that inherits from **InputMethodExtensionAbility**.
25
26```ts
27import { InputMethodExtensionAbility } from '@kit.IMEKit';
28import { Want } from '@kit.AbilityKit';
29class InputMethodExtAbility extends InputMethodExtensionAbility {
30  onCreate(want: Want): void {
31    let context = this.context;
32  }
33}
34```
35
36## InputMethodExtensionContext.destroy
37
38destroy(callback: AsyncCallback&lt;void&gt;): void;
39
40Destroys this input method. This API uses an asynchronous callback to return the result.
41
42**System capability**: SystemCapability.MiscServices.InputMethodFramework
43
44**Parameters**
45
46| Name  | Type                | Mandatory| Description                                                        |
47| -------- | -------------------- | ---- | ------------------------------------------------------------ |
48| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
49
50**Example**
51
52```ts
53import { InputMethodExtensionAbility } from '@kit.IMEKit';
54import { Want } from '@kit.AbilityKit';
55import { BusinessError } from '@kit.BasicServicesKit';
56
57class InputMethodExtAbility extends InputMethodExtensionAbility {
58  onCreate(want: Want): void {
59    let context = this.context;
60  }
61  onDestroy() {
62    this.context.destroy((err: BusinessError) => {
63      if(err) {
64        console.error(`Failed to destroy context, err code = ${err.code}`);
65        return;
66      }
67      console.info('Succeeded in destroying context.');
68    });
69  }
70}
71```
72
73## InputMethodExtensionContext.destroy
74
75destroy(): Promise&lt;void&gt;;
76
77Destroys this input method. This API uses a promise to return the result.
78
79**System capability**: SystemCapability.MiscServices.InputMethodFramework
80
81**Return value**
82
83| Type| Description|
84| -------- | -------- |
85| Promise\<void> | Promise that returns no value.|
86
87**Example**
88
89```ts
90import { InputMethodExtensionAbility } from '@kit.IMEKit';
91import { Want } from '@kit.AbilityKit';
92import { BusinessError } from '@kit.BasicServicesKit';
93
94class InputMethodExtAbility extends InputMethodExtensionAbility {
95  onCreate(want: Want): void {
96    let context = this.context;
97  }
98  onDestroy() {
99    this.context.destroy().then(() => {
100      console.info('Succeed in destroying context.');
101    }).catch((err: BusinessError)=>{
102      console.error(`Failed to destroy context, err code = ${err.code}`);
103    });
104  }
105}
106```
107
108## InputMethodExtensionContext.startAbility<sup>12+</sup>
109
110startAbility(want: Want): Promise&lt;void&gt;;
111
112Starts an ability. This API uses a promise to return the result.
113
114**System capability**: SystemCapability.MiscServices.InputMethodFramework
115
116**Parameters**
117
118| Name| Type                                                   | Mandatory| Description                                                        |
119| ------ | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
120| want   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Want information related to the extension ability to start, including the ability name and bundle name.|
121
122**Return value**
123
124| Type          | Description                     |
125| -------------- | ------------------------- |
126| Promise\<void> | Promise that returns no value.|
127
128**Error codes**
129
130For details about the error codes, see [Input Method Framework Error Codes](errorcode-inputmethod-framework.md), [Ability Error Codes](../apis-ability-kit/errorcode-ability.md), and [Universal Error Codes](../errorcode-universal.md).
131
132| ID| Error Message                                               |
133| -------- | ------------------------------------------------------- |
134| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
135| 16000001 | The specified ability does not exist.                   |
136| 16000002 | Incorrect ability type.                                 |
137| 16000004 | Cannot start an invisible component.                    |
138| 16000005 | The specified process does not have the permission.     |
139| 16000006 | Cross-user operations are not allowed.                  |
140| 16000008 | The crowdtesting application expires.                   |
141| 16000009 | An ability cannot be started or stopped in Wukong mode. |
142| 16000010 | The call with the continuation and prepare continuation flag is forbidden.       |
143| 16000011 | The context does not exist.                             |
144| 16000012 | The application is controlled.                          |
145| 16000013 | The application is controlled by EDM.                   |
146| 16000019 | No matching ability is found.                            |
147| 16000050 | Internal error.                                         |
148| 16000053 | The ability is not on the top of the UI.                |
149| 16000055 | Installation-free timed out.                            |
150| 16000061 | Operation not supported.                                |
151| 16200001 | The caller has been released.                           |
152| 16000069 | The extension cannot start the third party application. |
153| 16000070 | The extension cannot start the service.                 |
154
155**Example**
156
157```ts
158import { InputMethodExtensionAbility } from '@kit.IMEKit';
159import { Want } from '@kit.AbilityKit';
160import { BusinessError } from '@kit.BasicServicesKit';
161
162class InputMethodExtAbility extends InputMethodExtensionAbility {
163  onCreate(want: Want): void {
164    const context = this.context;
165    const targetWant: Want = {
166      bundleName: "com.example.aafwk.test",
167      abilityName: "com.example.aafwk.test.TwoAbility"
168    };
169
170    context.startAbility(targetWant)
171      .then(() => console.info('startAbility success'))
172      .catch((err: BusinessError) => {
173        console.error(`StartAbility failed. Code: ${err.code}, Message: ${err.message}`);
174      });
175  }
176  onDestroy() {
177    this.context.destroy().then(() => {
178      console.info('Succeed in destroying context.');
179    }).catch((err: BusinessError)=>{
180      console.error(`Failed to destroy context, err code = ${err.code}`);
181    });
182  }
183}
184```
185