I have written a piece of Java code which is intended as to call Java code from Javascript by a android WebView gateway.
webView = (WebView) findViewById(R.id.captchaView);
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
webView.loadUrl("https://google.com/");
webView.setWebViewClient(BrowserHandler);
webView.addJavascriptInterface(new IJavascriptHandler(), "cpjs");
WebViewClient BrowserHandler = new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.cpjs.onPageLoaded(document.body.innerHTML);void(0);");
}
};
final class IJavascriptHandler {
IJavascriptHandler() {
}
public void onPageLoaded(String html) {
// this is called from JS
Toast t = Toast.makeText(getApplicationContext(), "Yes", 2000);
t.show();
}
}
The above code checks for web page load and once the page is finished loading, it will call javascript function which is exposed from Java so it could carry what I need from the page.
This is not working as expected what it does is simply refresh the page every few seconds. I am not sure what is wrong here?