openharmony 鸿蒙 arkts-apis-web-webNativeMessagingExtensionContext

2026-08-25 浏览 (1)

@ohos.web.WebNativeMessagingExtensionContext (Web Native Messaging Extension Context)

WebNativeMessagingExtensionContext是Web原生消息扩展的上下文,继承自ExtensionContext。它提供了与WebNativeMessagingExtension通信消息的交互能力。

说明:

本模块首批接口从API version 21开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅可在Stage模型下使用。

导入模块

import { WebNativeMessagingExtensionAbility, ConnectionInfo } from '@kit.ArkWeb';

WebNativeMessagingExtensionContext

WebNativeMessagingExtensionContext是Web原生消息扩展的上下文,包含所需交互能力。

startAbility

startAbility(want: Want, options?: StartOptions): Promise<void>

使用Promise异步回调启动Ability。

系统能力: SystemCapability.Web.Webview.Core

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
wantWant表示需要启动的Ability的信息。
optionsStartOptions启动选项。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID错误信息
201The application does not have permission to call the interface.
16000001The specified ability does not exist.
16000002Incorrect ability type.
16000004Cannot start an invisible component.
16000005The specified process does not have the permission.
16000008The crowdtesting application expires.
16000009An ability cannot be started or stopped in Wukong mode.
16000010The call with the continuation and prepare continuation flag is forbidden.
16000011The context does not exist.
16000012The application is controlled.
16000013The application is controlled by EDM.
16000019No matching ability is found.
16000050Internal error. Possible causes: 1. Failed to connect to the system service; 2. The system service failed to communicate with dependency module.
16000055Installation-free timed out.
16000071App clone is not supported.
16000072App clone or multi-instance is not supported.
16000073The app clone index is invalid.
16000076The app instance key is invalid.
16000077The number of app instances reaches the limit.
16000078The multi-instance is not supported.
16000079The APP_INSTANCE_KEY cannot be specified.
16000080Creating a new instance is not supported.

示例:

import { WebNativeMessagingExtensionAbility, ConnectionInfo } from '@kit.ArkWeb';
import { Want } from '@kit.AbilityKit';

export class MyWebNativeMessagingExtension extends WebNativeMessagingExtensionAbility {
  onConnectNative(info: ConnectionInfo): void {
    const abilityWant: Want = {
    bundleName: 'com.example.mybundle',
    abilityName: 'MainAbility'
    };
    try {
        const context = this.context; // 获取 WebNativeMessagingExtensionContext 实例
        context.startAbility(abilityWant);
        console.info('Ability started successfully');
    } catch (err) {
        console.error(`Failed to start ability. Code: ${err.code}, Message: ${err.message}`);
    }
  }
}

startAbilityForResult

startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>

启动一个UIAbility,开发者可以通过回调函数接收被拉起的UIAbility退出时的返回结果。使用Promise异步回调。

UIAbility被启动后,有如下情况:

  • 正常情况下可通过调用terminateSelfWithResult接口使之终止并且返回结果给调用方。
  • 异常情况下比如销毁UIAbility会返回异常信息给调用方,异常信息中resultCode为-1。
  • 只支持拉起自己应用的UIAbility。

起始版本: 26.0.0

系统能力: SystemCapability.Web.Webview.Core

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
wantWant表示需要启动的UIAbility的信息。
optionsStartOptions启动选项。

返回值:

类型说明
Promise<AbilityResult>Promise对象,返回被启动方退出时的结果码和数据。

错误码:

以下错误码详细介绍请参考通用错误码元能力子系统错误码

错误码ID错误信息
201The application does not have permission to call the interface.
16000001The specified ability does not exist.
16000002Incorrect ability type.
16000004Cannot start an invisible component.
16000005The specified process does not have the permission.
16000008The crowdtesting application expires.
16000009An ability cannot be started or stopped in Wukong mode.
16000010The call with the continuation and prepare continuation flag is forbidden.
16000011The context does not exist.
16000012The application is controlled by the AppGallery and cannot be started.
16000013The application is controlled by Enterprise Device Manager and cannot be started.
16000019No matching ability is found.
16000050Internal error. Possible causes: 1. Failed to connect to the system service; 2. The system service failed to communicate with dependency module.
16000055Installation-free timed out.
16000071The application does not support appClone mode in multiAppMode.
16000072The application does not support appClone and multi-instance mode in multiAppMode.
16000073The app clone index is invalid.
16000076The app instance key is invalid.
16000077The number of app instances reaches the limit.
16000078The application does not support multiple instances.
16000079The APP_INSTANCE_KEY cannot be specified.
16000080Instances cannot be created for other applications during inter-application startup.

示例:

import { WebNativeMessagingExtensionAbility, ConnectionInfo } from '@kit.ArkWeb';
import { Want, common } from '@kit.AbilityKit';

export class MyWebNativeMessagingExtension extends WebNativeMessagingExtensionAbility {
  onConnectNative(info: ConnectionInfo): void {
    const abilityWant: Want = {
      bundleName: 'com.example.mybundle', // 请开发者替换为实际的 bundleName
      abilityName: 'MainAbility' // 请开发者替换为实际的 abilityName
    };
    try {
      const context = this.context; // 获取 WebNativeMessagingExtensionContext 实例
      context.startAbilityForResult(abilityWant).then((result: common.AbilityResult) => {
        console.info(`Ability started successfully, result code: ${result.resultCode}`);
        if (result.want) {
          console.info(`Result data: ${JSON.stringify(result.want)}`);
        }
      }).catch((err: Error) => {
        console.error(`Failed to start ability. Code: ${err.name}, Message: ${err.message}`);
      });
    } catch (err) {
      console.error(`Failed to start ability. Code: ${(err as Error).name}, Message: ${(err as Error).message}`);
    }
  }
}

terminateSelf

terminateSelf(): Promise<void>

销毁当前Web原生消息扩展。该方法返回一个Promise对象用于异步处理。

系统能力: SystemCapability.Web.Webview.Core

模型约束: 此接口仅可在Stage模型下使用。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考元能力子系统错误码

错误码ID错误信息
16000009An ability cannot be started or stopped in Wukong mode.
16000011The context does not exist.
16000050Internal error. Possible causes: 1. Failed to connect to the system service; 2. The system service failed to communicate with dependency module.

示例:

import { WebNativeMessagingExtensionAbility, ConnectionInfo } from '@kit.ArkWeb';

export class MyWebNativeMessagingExtension extends WebNativeMessagingExtensionAbility {
  onConnectNative(info: ConnectionInfo): void {
    try {
        const context = this.context; // 获取 WebNativeMessagingExtensionContext 实例
        context.terminateSelf();
        console.info('Extension terminated successfully');
    } catch (err) {
        console.error(`Failed to terminate extension. Code: ${err.code}, Message: ${err.message}`);
    }
  }
}

stopNativeConnection

stopNativeConnection(connectionId: number): Promise<void>

停止指定的本地连接。使用Promise进行异步回调。

系统能力: SystemCapability.Web.Webview.Core

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名类型必填说明
connectionIdnumber要停止的连接ID。取值范围为正整数,必须是有效的连接ID。当connectionId值无效时,会对应返回错误码。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码详细介绍请参考通用错误码

错误码ID错误信息
201The application does not have permission to call the interface.
16000011The context does not exist.
16000050Internal error. Possible causes: 1. Failed to connect to the system service; 2. The system service failed to communicate with dependency module.

示例:

import { WebNativeMessagingExtensionAbility, ConnectionInfo } from '@kit.ArkWeb';

export class MyWebNativeMessagingExtension extends WebNativeMessagingExtensionAbility {
  onConnectNative(info: ConnectionInfo): void {
    const CONNECTION_ID = 12345; // 实际的连接 ID
    try {
        const context = this.context;// 获取 WebNativeMessagingExtensionContext 实例
        context.stopNativeConnection(CONNECTION_ID);
        console.info('Native connection stopped successfully');
    } catch (err) {
        console.error(`Failed to stop native connection. Code: ${err.code}, Message: ${err.message}`);
    }
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 arkts-apis-webview-WebviewController

openharmony 鸿蒙 arkts-apis-webview-t

openharmony 鸿蒙 arkts-apis-webview

openharmony 鸿蒙 capi-arkweb-type-h

openharmony 鸿蒙 arkts-basic-components-web-FullScreenExitHandler

openharmony 鸿蒙 arkts-apis-webview-WebDownloadManager

openharmony 鸿蒙 arkts-basic-components-web-VerifyPinHandler

openharmony 鸿蒙 capi-web-arkweb-webmessage8h

openharmony 鸿蒙 arkts-apis-webview-NativeMediaPlayerHandler

openharmony 鸿蒙 capi-web-arkweb-proxymethodwithresult

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