openharmony 鸿蒙 js-apis-selectionInput-selectionExtensionAbility

2026-08-25 浏览 (1)

@ohos.selectionInput.SelectionExtensionAbility (SelectionExtensionAbility)

This module provides ExtensionAbility for word selection, allowing users to search or translate text selected using a mouse or touchpad.

NOTE

  • The initial APIs of this module are supported since API version 24. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • This module is supported only on PCs/2-in-1 devices.

Modules to Import

import { SelectionExtensionAbility } from '@kit.BasicServicesKit';

SelectionExtensionAbility

System capability: SystemCapability.SelectionInput.Selection

Model restriction: This API can be used only in the stage model.

NameTypeRead-OnlyOptionalDescription
contextSelectionExtensionContextNoNoContext of the SelectionExtensionAbility. This context is inherited from ExtensionContext.

onConnect

onConnect(want: Want): rpc.RemoteObject

Called when the SelectionExtensionAbility instance is created. You can execute initialization logic (such as defining variables, loading resources, and listening for word selection events) within this callback.

System capability: SystemCapability.SelectionInput.Selection

Model restriction: This API can be used only in the stage model.

Parameters

NameTypeMandatoryDescription
wantWantYesWant information of the SelectionExtensionAbility, including the ability name and bundle name.

Return value

TypeDescription
rpc.RemoteObjectRemote object, which is used for communication between the client and server.

Example

import { SelectionExtensionAbility } from '@kit.BasicServicesKit';
import { rpc } from '@kit.IPCKit';
import { Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG: string = '[SelectionExtensionAbility]';

class StubTest extends rpc.RemoteObject {
  constructor(des: string) {
    super(des);
  }
  onConnect(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) {
  }
}

class ServiceExtAbility extends SelectionExtensionAbility {
  onConnect(want: Want): rpc.RemoteObject {
    hilog.info(0x0000, TAG, `onConnect, want: ${want.abilityName}`);
    return new StubTest('test');
  }
}

onDisconnect

onDisconnect(): void

Called when the SelectionExtensionAbility instance is destroyed (for example, when the user disables the word selection function or switches the word selection application). You can clear resources and save data during this lifecycle. This API returns the result synchronously or uses a promise to return the result.

After the onDisconnect() lifecycle callback is executed, the application may exit. Consequently, the asynchronous function (for example, asynchronously writing data to the database) in onDisconnect() may fail to be executed. Using a Promise for asynchronous callback is recommended to prevent such issues.

The callback is invoked only when the SelectionExtensionAbility exits gracefully. It is not invoked in cases of abnormal exits (for example, process termination due to low memory conditions).

System capability: SystemCapability.SelectionInput.Selection

Model restriction: This API can be used only in the stage model.

Example

import { SelectionExtensionAbility } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG: string = '[SelectionExtensionAbility]';

class ServiceExtAbility extends SelectionExtensionAbility {
  onDisconnect(): void {
    hilog.info(0x0000, TAG, `onDisconnect`);
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 errorcode-zlib

openharmony 鸿蒙 capi-oh-scan-scan-picturescanprogress

openharmony 鸿蒙 js-apis-screen-lock

openharmony 鸿蒙 capi-os-account-common-h

openharmony 鸿蒙 js-apis-screen-lock-sys

openharmony 鸿蒙 capi-oh-print-print-propertylist

openharmony 鸿蒙 capi-oh-scan

openharmony 鸿蒙 js-apis-system-device

openharmony 鸿蒙 errorcode-intelligentVoice

openharmony 鸿蒙 js-apis-request-sys

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/lsYyW6Or