js-apis-inputmethod-extension-context.md 2.2 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.inputmethodextensioncontext (InputMethodExtensionContext)
E
ester.zhou 已提交
2 3 4 5 6 7

The **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.

> **NOTE**
E
ester.zhou 已提交
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. 
E
ester.zhou 已提交
10 11 12 13 14 15 16 17 18 19 20 21

## Modules to Import

```
import InputMethodExtensionContext from '@ohos.inputmethodextensioncontext';
```

## Usage

Before using the **InputMethodExtensionContext** module, you must define a child class that inherits from **InputMethodExtensionAbility**.

```js
E
ester.zhou 已提交
22
import InputMethodExtensionAbility from '@ohos.inputmethodextensionability';
E
ester.zhou 已提交
23
class EntryAbility extends InputMethodExtensionAbility {
E
ester.zhou 已提交
24 25 26 27
    onCreate() {
        let context = this.context;
    }
}
E
ester.zhou 已提交
28 29
```

E
ester.zhou 已提交
30
## InputMethodExtensionContext.destroy
E
ester.zhou 已提交
31

E
ester.zhou 已提交
32
destroy(callback: AsyncCallback\<void>): void
E
ester.zhou 已提交
33 34 35 36 37 38 39

Terminates this ability. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

E
ester.zhou 已提交
40 41 42
| Name  | Type                | Mandatory| Description                                                        |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
E
ester.zhou 已提交
43 44 45

**Example**

E
ester.zhou 已提交
46 47 48
```js
this.context.destroy((err) => {
    console.log('destroy result:' + JSON.stringify(err));
E
ester.zhou 已提交
49
});
E
ester.zhou 已提交
50
```
E
ester.zhou 已提交
51

E
ester.zhou 已提交
52
## InputMethodExtensionContext.destroy
E
ester.zhou 已提交
53

E
ester.zhou 已提交
54
destroy(): Promise<void&gt;
E
ester.zhou 已提交
55 56 57 58 59 60 61

Terminates this ability. This API uses a promise to return the result.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Return value**

E
ester.zhou 已提交
62 63 64
| Type| Description|
| -------- | -------- |
| Promise<void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
65 66 67

**Example**

E
ester.zhou 已提交
68 69 70 71 72 73 74
```js
this.context.destroy().then((data) => {
    console.log('success:' + JSON.stringify(data));
}).catch((error) => {
    console.log('failed:' + JSON.stringify(error));
});
```