gpt4 book ai didi

android - webview 没有关闭 onclosewindow

转载 作者:行者123 更新时间:2023-11-29 23:27:00 32 4
gpt4 key购买 nike

我成功创建了一个出色的 webview 应用程序,但遇到了一些挑战:

弹出窗口在 newwebview 中打开,例如 comm100 聊天窗口,遗憾的是我无法在聊天结束时关闭窗口。不确定要添加到 onPageFinished() 和/或 onCloseWindow()

非常感谢任何帮助。

public class MainActivity extends AppCompatActivity {
private WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView=(WebView) findViewById(R.id.view1);
webView.loadUrl("https://comm100.com");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient(){

@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
final WebView newWebview = new WebView(MainActivity.this);
newWebview.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
WebSettings webSettings1 = newWebview.getSettings();
webSettings1.setJavaScriptEnabled(true);
webSettings1.setDomStorageEnabled(true);
webSettings1.setSupportMultipleWindows(true);
webSettings1.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings1.setAllowFileAccess(true);
webSettings1.setAllowContentAccess(true);
webSettings1.setAllowUniversalAccessFromFileURLs(true);
webSettings1.setAllowFileAccessFromFileURLs(true);
view.addView(newWebview);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebview);
resultMsg.sendToTarget();
newWebview.setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);
}

});
//return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
return true;
}

@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}
});

}


@Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
} else {
super.onBackPressed();
}
}
}

Logcat 显示以下错误

I/chromium: [INFO:CONSOLE(8)] "Uncaught (in promise) #<Object>", source: https://chatserver.comm100.com/js/bundle.4273ade4b401f37d4797b68863b403e6.js (8)

查看视频示例 Video Link

最佳答案

通过将 WebChromeClient 添加到 onCreateWindow 覆盖来修复

    public class MainActivity extends AppCompatActivity {
private WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView=(WebView) findViewById(R.id.view1);
webView.loadUrl("https://comm100.com");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient(){

// popup webview!
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, final Message resultMsg) {
final WebView newWebView = new WebView(MainActivity.this);
newWebView.getSettings().setJavaScriptEnabled(true);
newWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
newWebView.getSettings().setSupportMultipleWindows(true);
newWebView.getSettings().setDomStorageEnabled(true);
newWebView.getSettings().setAllowFileAccess(true);
newWebView.getSettings().setAllowContentAccess(true);
newWebView.getSettings().setAllowFileAccessFromFileURLs(true);
newWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
newWebView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); //making sure the popup opens full screen
view.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
newWebView.setWebChromeClient(new WebChromeClient(){
@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
if (newWebView !=null){
webView.removeView(newWebView);
}
}
});
return true;
}
@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}
});

}


@Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
} else {
super.onBackPressed();
}
}
}

关于android - webview 没有关闭 onclosewindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53393002/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com