在webview中,使用对话框启动带有window.open的新窗口。 按下智能手机的返回键时,尝试如下运行“ javascript:self.close()”。
newWebView.evaluateJavaScript ("self.close()", null);
顶部有错误。
error: cannot find symbol method evaluateJavaScript (String, <null>)
因此,我将其修改为一个对话框,但是我看到了相同的错误。
dialog.evaluateJavaScript ("self.close()", null);
按下返回键并用dialog.dismiss();
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(final WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(MainActivity.this);
WebSettings webSettings = newWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
newWebView.setWebViewClient(new WebViewClient());
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(newWebView);
dialog.show();
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
showGuide("HOME pressed...");
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
//dialog.dismiss();
//dialog.cancel();
// Build.VERSION.SDK_INT : 25
// minSdkVersion 15
// targetSdkVersion 28
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
try {
dialog.evaluateJavaScript("self.close()", null); // 여기서 에러가 납니다.
} catch (Exception e) {
Log.d("scriptCall", e.toString());
}
}
});
showGuide("KEYCODE_BACK pressed...");
return true;
}
return false;
}
});
newWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
dialog.dismiss();
}
});
((WebView.WebViewTransport) resultMsg.obj).setWebView(newWebView);
resultMsg.sendToTarget();
return true;
}
});