WebMessagePort


public abstract class WebMessagePort
extends Object

java.lang.Object
   ↳ android.webkit.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

class WebMessagePort.WebMessageCallback

The listener for handling MessagePort events. 

Public methods

abstract void close()

Close the message port and free any resources associated with it.

abstract void postMessage(WebMessage message)

Post a WebMessage to the entangled port.

abstract void setWebMessageCallback(WebMessagePort.WebMessageCallback callback, Handler handler)

Sets a callback to receive message events on the handler that is provided by the application.

abstract void setWebMessageCallback(WebMessagePort.WebMessageCallback callback)

Sets a callback to receive message events on the main thread.

Inherited methods

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public methods

close

Added in API level 23
public abstract void close ()

Close the message port and free any resources associated with it.

postMessage

Added in API level 23
public abstract void postMessage (WebMessage message)

Post a WebMessage to the entangled port.

Parameters
message WebMessage: the message from Java to JS.

Throws
IllegalStateException If message port is already transferred or closed.

setWebMessageCallback

Added in API level 23
public abstract void setWebMessageCallback (WebMessagePort.WebMessageCallback callback, 
                Handler handler)

Sets a callback to receive message events on the handler that is provided by the application.

Parameters
callback WebMessagePort.WebMessageCallback: the message callback.

handler Handler: the handler to receive the message messages.

setWebMessageCallback

Added in API level 23
public abstract void setWebMessageCallback (WebMessagePort.WebMessageCallback callback)

Sets a callback to receive message events on the main thread.

Parameters
callback WebMessagePort.WebMessageCallback: the message callback.