openharmony 鸿蒙 arkts-apis-web-webNativeMessagingExtensionContext

2026-08-25 浏览 (1)

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

WebNativeMessagingExtensionContext is the context of web native message extension and is inherited from ExtensionContext. It provides the capability of exchanging messages with WebNativeMessagingExtension.

NOTE

The initial APIs of this module are supported since API version 21. 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 { WebNativeMessagingExtensionAbility, ConnectionInfo } from '@kit.ArkWeb';

WebNativeMessagingExtensionContext

Represents the context of web native message extension, including the required interaction capabilities.

startAbility

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

Starts an ability using a promise.

System capability: SystemCapability.Web.Webview.Core

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

Parameters

NameTypeMandatoryDescription
wantWantYesInformation about the ability to start.
optionsStartOptionsNoStartup options.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
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.

Example

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; // Obtain the WebNativeMessagingExtensionContext instance.
        context.startAbility(abilityWant);
        console.info('Ability started successfully');
    } catch (err) {
        console.error(`Failed to start ability. Code: ${err.code}, Message: ${err.message}`);
    }
  }
}

terminateSelf

terminateSelf(): Promise<void>

Destroys the current native web message extension. This method returns a promise for asynchronous processing.

System capability: SystemCapability.Web.Webview.Core

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

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Ability Error Codes.

IDError Message
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.

Example

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

export class MyWebNativeMessagingExtension extends WebNativeMessagingExtensionAbility {
  onConnectNative(info: ConnectionInfo): void {
    try {
        const context = this.context; // Obtain the WebNativeMessagingExtensionContext instance.
        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>

Stops a native connection. This API uses a promise to return the result.

System capability: SystemCapability.Web.Webview.Core

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

Parameters

NameTypeMandatoryDescription
connectionIdnumberYesID of the connection to stop.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
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.

Example

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

export class MyWebNativeMessagingExtension extends WebNativeMessagingExtensionAbility {
  onConnectNative(info: ConnectionInfo): void {
    const CONNECTION_ID = 12345; // Actual connection ID.
    try {
        const context = this.context;// Obtain the WebNativeMessagingExtensionContext instance.
        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/AIIu6Wfj