Stay organized with collections
Save and categorize content based on your preferences.
WebMessagePort
abstract class WebMessagePort
The Java representation of the HTML5 message ports.
A Message port represents one endpoint of a Message Channel. In Android webview, there is no separate Message Channel object. When a message channel is created, both ports are tangled to each other and started, and then returned in a MessagePort array, see WebView.createWebMessageChannel
for creating a message channel.
When a message port is first created or received via transfer, it does not have a WebMessageCallback to receive web messages. The messages are queued until a WebMessageCallback is set.
A message port should be closed when it is not used by the embedder application anymore. A closed port cannot be transferred or cannot be reopened to send messages. Close can be called multiple times.
When a port is transferred to JS, it cannot be used to send or receive messages at the Java side anymore. Different from HTML5 Spec, a port cannot be transferred if one of these has ever happened: i. a message callback was set, ii. a message was posted on it. A transferred port cannot be closed by the application, since the ownership is also transferred.
It is possible to transfer both ports of a channel to JS, for example for communication between subframes.
Summary
Nested classes |
abstract |
The listener for handling MessagePort events.
|
Public methods |
abstract Unit |
Close the message port and free any resources associated with it.
|
abstract Unit |
Post a WebMessage to the entangled port.
|
abstract Unit |
Sets a callback to receive message events on the main thread.
|
abstract Unit |
Sets a callback to receive message events on the handler that is provided by the application.
|
Public methods
close
abstract fun close(): Unit
Close the message port and free any resources associated with it.
postMessage
abstract fun postMessage(message: WebMessage!): Unit
Post a WebMessage to the entangled port.
Parameters |
message |
WebMessage!: the message from Java to JS. |
Exceptions |
java.lang.IllegalStateException |
If message port is already transferred or closed. |
setWebMessageCallback
abstract fun setWebMessageCallback(
callback: WebMessagePort.WebMessageCallback!,
handler: Handler!
): Unit
Sets a callback to receive message events on the handler that is provided by the application.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# WebMessagePort\n\nAdded in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nWebMessagePort\n==============\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/webkit/WebMessagePort \"View this page in Java\") \n\n```\nabstract class WebMessagePort\n```\n\n|---|------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.webkit.WebMessagePort](#) |\n\nThe Java representation of the [HTML5 message ports.](https://html.spec.whatwg.org/multipage/comms.html#messageport)\n\nA Message port represents one endpoint of a Message Channel. In Android webview, there is no separate Message Channel object. When a message channel is created, both ports are tangled to each other and started, and then returned in a MessagePort array, see [WebView.createWebMessageChannel](/reference/kotlin/android/webkit/WebView#createWebMessageChannel()) for creating a message channel.\n\nWhen a message port is first created or received via transfer, it does not have a WebMessageCallback to receive web messages. The messages are queued until a WebMessageCallback is set.\n\nA message port should be closed when it is not used by the embedder application anymore. A closed port cannot be transferred or cannot be reopened to send messages. Close can be called multiple times.\n\nWhen a port is transferred to JS, it cannot be used to send or receive messages at the Java side anymore. Different from HTML5 Spec, a port cannot be transferred if one of these has ever happened: i. a message callback was set, ii. a message was posted on it. A transferred port cannot be closed by the application, since the ownership is also transferred.\n\nIt is possible to transfer both ports of a channel to JS, for example for communication between subframes.\n\nSummary\n-------\n\n| Nested classes ||\n|----------|----------------------------------------------------------------------------------------------------------------------------------------|\n| abstract | [WebMessageCallback](/reference/kotlin/android/webkit/WebMessagePort.WebMessageCallback) The listener for handling MessagePort events. |\n\n| Public methods ||\n|---------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [close](#close())`()` Close the message port and free any resources associated with it. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [postMessage](#postMessage(android.webkit.WebMessage))`(`message:` `[WebMessage](/reference/kotlin/android/webkit/WebMessage)!`)` Post a WebMessage to the entangled port. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [setWebMessageCallback](#setWebMessageCallback(android.webkit.WebMessagePort.WebMessageCallback))`(`callback:` `[WebMessagePort.WebMessageCallback](/reference/kotlin/android/webkit/WebMessagePort.WebMessageCallback)!`)` Sets a callback to receive message events on the main thread. |\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [setWebMessageCallback](#setWebMessageCallback(android.webkit.WebMessagePort.WebMessageCallback,%20android.os.Handler))`(`callback:` `[WebMessagePort.WebMessageCallback](/reference/kotlin/android/webkit/WebMessagePort.WebMessageCallback)!`, `handler:` `[Handler](../os/Handler.html#)!`)` Sets a callback to receive message events on the handler that is provided by the application. |\n\nPublic methods\n--------------\n\n### close\n\nAdded in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun close(): Unit\n```\n\nClose the message port and free any resources associated with it. \n\n### postMessage\n\nAdded in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun postMessage(message: WebMessage!): Unit\n```\n\nPost a WebMessage to the entangled port.\n\n| Parameters ||\n|-----------|------------------------------------------------------------------------------------------|\n| `message` | [WebMessage](/reference/kotlin/android/webkit/WebMessage)!: the message from Java to JS. |\n\n| Exceptions ||\n|-----------------------------------|---------------------------------------------------|\n| `java.lang.IllegalStateException` | If message port is already transferred or closed. |\n\n### setWebMessageCallback\n\nAdded in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun setWebMessageCallback(callback: WebMessagePort.WebMessageCallback!): Unit\n```\n\nSets a callback to receive message events on the main thread.\n\n| Parameters ||\n|------------|---------------------------------------------------------------------------------------------------------------------------------|\n| `callback` | [WebMessagePort.WebMessageCallback](/reference/kotlin/android/webkit/WebMessagePort.WebMessageCallback)!: the message callback. |\n\n### setWebMessageCallback\n\nAdded in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun setWebMessageCallback(\n callback: WebMessagePort.WebMessageCallback!, \n handler: Handler!\n): Unit\n```\n\nSets a callback to receive message events on the handler that is provided by the application.\n\n| Parameters ||\n|------------|---------------------------------------------------------------------------------------------------------------------------------|\n| `callback` | [WebMessagePort.WebMessageCallback](/reference/kotlin/android/webkit/WebMessagePort.WebMessageCallback)!: the message callback. |\n| `handler` | [Handler](../os/Handler.html#)!: the handler to receive the message messages. |"]]