webview没有关闭onclosewindow

时间:2018-11-20 12:28:00

标签: android webview popup

我设法创建了一个出色的Webview应用程序,并遇到了一些挑战:

弹出窗口在newwebview中打开,例如comm100聊天窗口,可惜聊天结束后我无法关闭该窗口。不知道要添加到onPageFinished()和或onCloseWindow()

的内容

非常感谢您的帮助。

LC_ALL=C

Logcat显示以下错误

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();
    }
}
}

查看视频示例Video Link

1 个答案:

答案 0 :(得分:1)

通过将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();
    }
}
}