I am currently working with a JS script in a WebView, where the script triggers an alert message to the WebView that I want to capture in my app. However, I am facing an issue where the onJSAlert
method is not being called and I am unable to use the @Override
annotation when defining the method.
The imports in my code include -
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.JsResult;
import android.webkit.WebSettings;
The WebView setup looks 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");
The HTML code for test.html is as follows -
<html>
<body >
<div onclick="alert('CLICK')"> Click Me !! </div>
</body>
</html>
Upon running the app, the HTML file loads successfully and the onProgressChanged
method logs "finished loading". However, clicking on the link within the loaded page does not trigger the onJSAlert
method - instead, a dialog window appears on the device displaying the alert message.
Attempting to add the @Override
annotation to the onJSAlert
method results in an error stating "The method onJSAlert(WebView, String, String, JsResult) of type MainActivity.MyWebClient must override or implement a supertype method".
Any suggestions?