Really do not have any ideal why this doesn't work. Basically, I am trying to do this.
public void testJS() {
final WebView webView = (WebView) findViewById(web);
// webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JSInterface(webView), "sample");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
/* Do whatever you need here */
Log.e("chrome", "asdf");
return super.onJsAlert(view, url, message, result);
}
});
webView.loadUrl("javascript:window.sample.doEchoTest();void(0);");
}
and this is the JSInterface
public class JSInterface {
WebView mAppView;
public JSInterface(WebView appView) {
this.mAppView = appView;
}
public void doEchoTest() {
Log.e("sample", "test details");
Toast toast = Toast.makeText(mAppView.getContext(), "sample test details", Toast.LENGTH_SHORT);
toast.show();
}
}
the javascript code never runs.
basic functions like alert works :
webView.loadUrl("javascript:alert('sample')");
why is webView.loadUrl("javascript:window.sample.doEchoTest();void(0);");
not working ?