WebMessageCompat
public class WebMessageCompat
The Java representation of the HTML5 PostMessage event. See https://html.spec.whatwg.org/multipage/comms.html#the-messageevent-interfaces for definition of a MessageEvent in HTML5.
Summary
Constants |
|
|---|---|
static final int |
Indicates the payload of WebMessageCompat is JavaScript ArrayBuffer. |
static final int |
TYPE_STRING = 0Indicates the payload of WebMessageCompat is String. |
Public constructors |
|---|
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Creates a WebMessage with JavaScript ArrayBuffer payload. |
WebMessageCompat(@Nullable String data)Creates a WebMessage with String payload. |
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Creates a WebMessage with JavaScript ArrayBuffer payload. |
WebMessageCompat(Creates a WebMessage with String payload. |
Public methods |
|
|---|---|
@NonNull byte[] |
Returns the ArrayBuffer data of message. |
@Nullable String |
getData()Returns the String data of the message. |
@Nullable WebMessagePortCompat[] |
getPorts()Returns the ports that are sent with the message, or |
int |
getType()Returns the payload type of the message. |
Constants
TYPE_ARRAY_BUFFER
public static final int TYPE_ARRAY_BUFFER = 1
Indicates the payload of WebMessageCompat is JavaScript ArrayBuffer.
TYPE_STRING
public static final int TYPE_STRING = 0
Indicates the payload of WebMessageCompat is String.
Public constructors
WebMessageCompat
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public WebMessageCompat(@NonNull byte[] arrayBuffer)
Creates a WebMessage with JavaScript ArrayBuffer payload.
| Parameters | |
|---|---|
@NonNull byte[] arrayBuffer |
the array buffer data of the message. |
WebMessageCompat
public WebMessageCompat(@Nullable String data)
Creates a WebMessage with String payload.
WebMessageCompat
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
public WebMessageCompat(
@NonNull byte[] arrayBuffer,
@Nullable WebMessagePortCompat[] ports
)
Creates a WebMessage with JavaScript ArrayBuffer payload.
WebMessageCompat
public WebMessageCompat(
@Nullable String data,
@Nullable WebMessagePortCompat[] ports
)
Creates a WebMessage with String payload.
Public methods
getArrayBuffer
public @NonNull byte[] getArrayBuffer()
Returns the ArrayBuffer data of message. A ArrayBuffer or Transferable ArrayBuffer can be received from JavaScript. This should only be called when getType returns TYPE_ARRAY_BUFFER. Example:
WebMessageCompat message = ... // The WebMessageCompat received or prepared.
if (message.getType() == WebMessageCompat.TYPE_ARRAY_BUFFER) {
byte[] arrayBuffer = message.getArrayBuffer();
// Access arrayBuffer data here.
}| Returns | |
|---|---|
@NonNull byte[] |
ArrayBuffer payload data. |
getData
public @Nullable String getData()
Returns the String data of the message. This should only be called when getType returns TYPE_STRING. Example:
WebMessageCompat message = ... // The WebMessageCompat received or prepared.
if (message.getType() == WebMessageCompat.TYPE_STRING) {
String string = message.getData();
// Access string data here.
}