未调用 WebChromeClient OnJSAlert
WebChromeClient OnJSAlert is not called
我正在 运行在 WebView 中创建一个 JS 脚本。该脚本向 WebView 发出一条消息提醒,我想在我的应用程序中接收它。问题是 onJSAlert
没有被调用,我也不能在定义方法时使用 @Override
注释。
我的进口商品是 -
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.JsResult;
import android.webkit.WebSettings;
我这样使用 WebView -
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");
我的 HTML test.html 代码是
<html>
<body >
<div onclick="alert('CLICK')"> Click Me !! </div>
</body>
</html>
当我 运行 应用程序时,会加载 HTML 文件并调用 onProgressChanged
- 我可以看到显示 "finished loading" 的日志消息。当我在加载的页面上单击 link 时,不会调用 onJSAlert
- 我没有收到日志消息,而是可以看到一个对话框 window 在我的设备上弹出带有警报消息。
如果我尝试在 onJSAlert
的开头使用 @Override
注释,我会收到错误消息 - "The method onJSAlert(WebView, String, String, JsResult) of type MainActivity.MyWebClient must override or implement a supertype method".
有什么想法吗?
如果您使用的方法名称拼写不正确,您的方法调用将不起作用
onJSAlert
更改为:
onJsAlert
我正在 运行在 WebView 中创建一个 JS 脚本。该脚本向 WebView 发出一条消息提醒,我想在我的应用程序中接收它。问题是 onJSAlert
没有被调用,我也不能在定义方法时使用 @Override
注释。
我的进口商品是 -
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.JsResult;
import android.webkit.WebSettings;
我这样使用 WebView -
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");
我的 HTML test.html 代码是
<html>
<body >
<div onclick="alert('CLICK')"> Click Me !! </div>
</body>
</html>
当我 运行 应用程序时,会加载 HTML 文件并调用 onProgressChanged
- 我可以看到显示 "finished loading" 的日志消息。当我在加载的页面上单击 link 时,不会调用 onJSAlert
- 我没有收到日志消息,而是可以看到一个对话框 window 在我的设备上弹出带有警报消息。
如果我尝试在 onJSAlert
的开头使用 @Override
注释,我会收到错误消息 - "The method onJSAlert(WebView, String, String, JsResult) of type MainActivity.MyWebClient must override or implement a supertype method".
有什么想法吗?
如果您使用的方法名称拼写不正确,您的方法调用将不起作用
onJSAlert
更改为:
onJsAlert