1# @ohos.InputMethodExtensionContext (InputMethodExtensionContext) 2 3The **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. 4 5> **NOTE** 6> 7> 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. 8> The APIs of this module can be used only in the stage model. 9 10## Modules to Import 11 12```ts 13import { InputMethodExtensionContext } from '@kit.IMEKit'; 14``` 15 16## Usage 17 18Before using the **InputMethodExtensionContext** module, you must define a child class that inherits from **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 34Destroys this input method. This API uses an asynchronous callback to return the result. 35 36**System capability**: SystemCapability.MiscServices.InputMethodFramework 37 38**Parameters** 39 40| Name | Type | Mandatory| Description | 41| -------- | -------------------- | ---- | ------------------------------------------------------------ | 42| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 43 44**Example** 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<void>; 70 71Destroys this input method. This API uses a promise to return the result. 72 73**System capability**: SystemCapability.MiscServices.InputMethodFramework 74 75**Return value** 76 77| Type| Description| 78| -------- | -------- | 79| Promise\<void> | Promise that returns no value.| 80 81**Example** 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<void>; 105 106Starts an ability. This API uses a promise to return the result. 107 108**System capability**: SystemCapability.MiscServices.InputMethodFramework 109 110**Parameters** 111 112| Name| Type | Mandatory| Description | 113| ------ | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 114| 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.| 115 116**Return value** 117 118| Type | Description | 119| -------------- | ------------------------- | 120| Promise\<void> | Promise that returns no value.| 121 122**Error codes** 123 124For 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). 125 126| ID| Error Message | 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**Example** 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