March 2013
Beginner to intermediate
580 pages
16h 11m
English
We saw how to use WebViewClient and WebChromeClient to respond to specific events that happen in your WebView. However, it is possible to do even more than that by injecting arbitrary JavaScript objects into the document contained in the WebView itself. Check out the documentation at http://developer.android.com/reference/android/webkit/WebView.html and scroll down to the addJavascriptInterface(Object,String) method. Using this, you can inject an arbitrary object into the document with a name you specify.
mWebView.addJavascriptInterface(new Object() {
public void send(String message) {
Log.i(TAG, "Received message: " + message);
}
}, "androidObject");
And then invoke it like so:
<input ...
Read now
Unlock full access