1# @ohos.InputMethodExtensionContext (InputMethodExtensionContext)
2
3InputMethodExtensionContext模块是InputMethodExtensionAbility的上下文环境,继承于ExtensionContext,提供InputMethodExtensionAbility具有的能力和接口,包括启动、停止、绑定、解绑Ability。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 本模块接口仅可在Stage模型下使用。
9
10## 导入模块
11
12```ts
13import { InputMethodExtensionContext } from '@kit.IMEKit';
14```
15
16## 使用说明
17
18在使用InputMethodExtensionContext的功能前,需要通过InputMethodExtensionAbility子类实例获取。
19
20```ts
21import { InputMethodExtensionAbility } from '@kit.IMEKit';
22import { Want } from '@kit.AbilityKit';
23class InputMethodExtnAbility extends InputMethodExtensionAbility {
24  onCreate(want: Want): void {
25    let context = this.context;
26  }
27}
28```
29
30## InputMethodExtensionContext.destroy
31
32destroy(callback: AsyncCallback<void>): void;
33
34销毁输入法应用。使用callback异步回调。
35
36**系统能力:** SystemCapability.MiscServices.InputMethodFramework
37
38**参数:**
39
40| 参数名   | 类型                 | 必填 | 说明                                                         |
41| -------- | -------------------- | ---- | ------------------------------------------------------------ |
42| callback | AsyncCallback\<void> | 是   | 回调函数。当销毁输入法应用成功时,err为undefined;否则为错误对象。 |
43
44**示例:**
45
46```ts
47import { InputMethodExtensionAbility } from '@kit.IMEKit';
48import { Want } from '@kit.AbilityKit';
49import { BusinessError } from '@kit.BasicServicesKit';
50
51class InputMethodExtnAbility extends InputMethodExtensionAbility {
52  onCreate(want: Want): void {
53    let context = this.context;
54  }
55  onDestroy() {
56    this.context.destroy((err: BusinessError) => {
57      if(err) {
58        console.log(`Failed to destroy context, err code = ${err.code}`);
59        return;
60      }
61      console.log('Succeeded in destroying context.');
62    });
63  }
64}
65```
66
67## InputMethodExtensionContext.destroy
68
69destroy(): Promise&lt;void&gt;;
70
71销毁输入法应用。使用Promise异步回调。
72
73**系统能力:** SystemCapability.MiscServices.InputMethodFramework
74
75**返回值:**
76
77| 类型 | 说明 |
78| -------- | -------- |
79| Promise\<void> | 无返回结果的Promise对象。 |
80
81**示例:**
82
83```ts
84import { InputMethodExtensionAbility } from '@kit.IMEKit';
85import { Want } from '@kit.AbilityKit';
86import { BusinessError } from '@kit.BasicServicesKit';
87
88class InputMethodExtnAbility extends InputMethodExtensionAbility {
89  onCreate(want: Want): void {
90    let context = this.context;
91  }
92  onDestroy() {
93    this.context.destroy().then(() => {
94      console.log('Succeed in destroying context.');
95    }).catch((err: BusinessError)=>{
96      console.log(`Failed to destroy context, err code = ${err.code}`);
97    });
98  }
99}
100```
101
102## InputMethodExtensionContext.startAbility<sup>12+</sup>
103
104startAbility(want: Want): Promise&lt;void&gt;;
105
106拉起目标应用。使用Promise异步回调。
107
108**系统能力:** SystemCapability.MiscServices.InputMethodFramework
109
110**参数:**
111
112| 参数名 | 类型                                                    | 必填 | 说明                                                         |
113| ------ | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
114| want   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 |
115
116**返回值:**
117
118| 类型           | 说明                      |
119| -------------- | ------------------------- |
120| Promise\<void> | 无返回结果的Promise对象。 |
121
122**错误码:**
123
124以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[元能力错误码](../apis-ability-kit/errorcode-ability.md),[通用错误码说明文档](../errorcode-universal.md)。
125
126| 错误码ID | 错误信息                                                |
127| -------- | ------------------------------------------------------- |
128| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
129| 16000001 | The specified ability does not exist.                   |
130| 16000002 | Incorrect ability type.                                 |
131| 16000004 | Can not start invisible component.                      |
132| 16000005 | The specified process does not have the permission.     |
133| 16000006 | Cross-user operations are not allowed.                  |
134| 16000008 | The crowdtesting application expires.                   |
135| 16000009 | An ability cannot be started or stopped in Wukong mode. |
136| 16000010 | The call with the continuation flag is forbidden.       |
137| 16000011 | The context does not exist.                             |
138| 16000012 | The application is controlled.                          |
139| 16000013 | The application is controlled by EDM.                   |
140| 16000019 | Can not match any component.                            |
141| 16000050 | Internal error.                                         |
142| 16000053 | The ability is not on the top of the UI.                |
143| 16000055 | Installation-free timed out.                            |
144| 16000061 | Can not start component belongs to other bundle.        |
145| 16200001 | The caller has been released.                           |
146| 16000069 | The extension cannot start the third party application. |
147| 16000070 | The extension cannot start the service.                 |
148
149**示例:**
150
151```ts
152import { InputMethodExtensionAbility } from '@kit.IMEKit';
153import { Want } from '@kit.AbilityKit';
154import { BusinessError } from '@kit.BasicServicesKit';
155
156class InputMethodExtnAbility extends InputMethodExtensionAbility {
157  onCreate(want: Want): void {
158    let context = this.context;
159  }
160  onDestroy() {
161    let want: Want = {
162      bundleName: "com.example.aafwk.test",
163      abilityName: "com.example.aafwk.test.TwoAbility"
164    };
165    try {
166      this.context.startAbility(want).then(() => {
167        console.log(`startAbility success`);
168      }).catch((err: BusinessError) => {
169        let error = err as BusinessError;
170        console.log(`startAbility error: ${error.code} ${error.message}`);
171      })
172    } catch (err) {
173      let error = err as BusinessError;
174      console.log(`startAbility error: ${error.code} ${error.message}`);
175    }
176  }
177}
178```
179
180