It is simple to work, but not obvious or well documented.
First you want to get bound to the Android object inside the webview. You can then use it to register one or more callbacks. For a simple example, we just make one message that displays a warning with a message from Python.
var droid = new Android(); droid.registerCallback("echo", function(msg) { alert(msg.data) });
In this case, echo is the name of the type of event with which you want to handle this callback. This way it will handle the βecho eventsβ. Event names are arbitrary strings, just name them, which makes sense.
In the Python script that launched the webview, you can now send events to the registered handler whenever you want.
droid.eventPost("echo", "hello world")
The second argument here is the message you want to pass to the JavaScript callback.
Note that although you pass the message as a string, it comes into the JavaScript function as an object. This object, which we call msg above, has a data attribute that contains the string that you passed from Python.
source share