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