harmony 鸿蒙@ohos.InputMethodExtensionContext (InputMethodExtensionContext)

  • 2025-06-12
  • 浏览 (2)

@ohos.InputMethodExtensionContext (InputMethodExtensionContext)

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

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. The APIs of this module can be used only in the stage model.

Modules to Import

import { InputMethodExtensionContext } from '@kit.IMEKit';

Usage

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

import { InputMethodExtensionAbility } from '@kit.IMEKit';
import { Want } from '@kit.AbilityKit';
class InputMethodExtnAbility extends InputMethodExtensionAbility {
  onCreate(want: Want): void {
    let context = this.context;
  }
}

InputMethodExtensionContext.destroy

destroy(callback: AsyncCallback<void>): void;

Destroys this input method. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

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.

Example

import { InputMethodExtensionAbility } from '@kit.IMEKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class InputMethodExtnAbility extends InputMethodExtensionAbility {
  onCreate(want: Want): void {
    let context = this.context;
  }
  onDestroy() {
    this.context.destroy((err: BusinessError) => {
      if(err) {
        console.log(`Failed to destroy context, err code = ${err.code}`);
        return;
      }
      console.log('Succeeded in destroying context.');
    });
  }
}

InputMethodExtensionContext.destroy

destroy(): Promise&lt;void&gt;;

Destroys this input method. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Return value

Type Description
Promise<void> Promise that returns no value.

Example

import { InputMethodExtensionAbility } from '@kit.IMEKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class InputMethodExtnAbility extends InputMethodExtensionAbility {
  onCreate(want: Want): void {
    let context = this.context;
  }
  onDestroy() {
    this.context.destroy().then(() => {
      console.log('Succeed in destroying context.');
    }).catch((err: BusinessError)=>{
      console.log(`Failed to destroy context, err code = ${err.code}`);
    });
  }
}

InputMethodExtensionContext.startAbility12+

startAbility(want: Want): Promise&lt;void&gt;;

Starts an ability. This API uses a promise to return the result.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

Name Type Mandatory Description
want Want Yes Want information related to the extension ability to start, including the ability name and bundle name.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Input Method Framework Error Codes, Ability Error Codes, and Universal Error Codes.

ID Error Message
401 parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000010 The call with the continuation flag is forbidden.
16000011 The context does not exist.
16000012 The application is controlled.
16000013 The application is controlled by EDM.
16000019 Can not match any component.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16000061 Can not start component belongs to other bundle.
16200001 The caller has been released.
16000069 The extension cannot start the third party application.
16000070 The extension cannot start the service.

Example

import { InputMethodExtensionAbility } from '@kit.IMEKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

class InputMethodExtnAbility extends InputMethodExtensionAbility {
  onCreate(want: Want): void {
    let context = this.context;
  }
  onDestroy() {
    let want: Want = {
      bundleName: "com.example.aafwk.test",
      abilityName: "com.example.aafwk.test.TwoAbility"
    };
    try {
      this.context.startAbility(want).then(() => {
        console.log(`startAbility success`);
      }).catch((err: BusinessError) => {
        let error = err as BusinessError;
        console.log(`startAbility error: ${error.code} ${error.message}`);
      })
    } catch (err) {
      let error = err as BusinessError;
      console.log(`startAbility error: ${error.code} ${error.message}`);
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙IME Kit

harmony 鸿蒙InputMethod

harmony 鸿蒙Input Method Framework Error Codes

harmony 鸿蒙inputmethod_attach_options_capi.h

harmony 鸿蒙inputmethod_controller_capi.h

harmony 鸿蒙inputmethod_cursor_info_capi.h

harmony 鸿蒙inputmethod_inputmethod_proxy_capi.h

harmony 鸿蒙inputmethod_private_command_capi.h

harmony 鸿蒙inputmethod_text_avoid_info_capi.h

harmony 鸿蒙inputmethod_text_config_capi.h

0  赞