I'm running a JS script in a WebView. The script alerts a message to the WebView, and I want to receive it in my app. The problem is that onJSAlert
is not called nor I can use @Override
annotaion when definig the method.
My imports are -
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.JsResult;
import android.webkit.WebSettings;
I use the WebView like this -
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
Log.d("fibi", "finished loading");
}
}
//@Override //If I uncomment this I get compilation error
public boolean onJSAlert(WebView view, String url, String message, final JsResult result) {
encResult = message;
Log.d("fibi", encResult);
result.cancel();
return true;
}
});
webView.loadUrl("file:///android_asset/test.html");
And my HTML code for test.html is
<html>
<body >
<div onclick="alert('CLICK')"> Click Me !! </div>
</body>
</html>
When I run the app, the HTML file loads and the onProgressChanged
is been called - I can see the log message that says "finished loading". When I click on the link at the loaded page, the onJSAlert
is not called - I don't get the log message and instead I can see a dialog window that pops on my device with the alert message.
If I try to use the @Override
annotaion at the beginning of the onJSAlert
, I get error - "The method onJSAlert(WebView, String, String, JsResult) of type MainActivity.MyWebClient must override or implement a supertype method".
Any ideas?
If you use a method name without the proper spelling, your method calls will not work
Change to: