这个看起来不错
http://www.gutterbling.com/blog/synchronous-javascript-evaluation-in-android-webview/
https://github.com/lolboxen/AndroidNIWebView/blob/master/NIWebView.java
试了一下。可以。但是要注意几个问题
见代码:
SynchronousJavascriptInterface jsInterface = new SynchronousJavascriptInterface();
wv.addJavascriptInterface(jsInterface, "SynchJS");
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
wv.loadData(summary, "text/html", null);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String jsResult2 = jsInterface.getJSValue(wv, "2 + 5");
Log.d("wjandroid","jsResult2="+jsResult2);
1.如果webview新建后没有loading过任何内容,将没有window对象。所以interface对象无法找到。
2. webview对js是异步调用。所以loading过一个内容后。下一个内容要等一会。或者异步处理。