处理来自onCreateWindow Web视图的URL

时间:2012-03-11 11:31:04

标签: android webview

我可以在webView上调用onCreateWindow获取url吗?

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg)
        {

        }

3 个答案:

答案 0 :(得分:9)

您可以使用此代码获得点击的网址。

@Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
            Message href = view.getHandler().obtainMessage();
            view.requestFocusNodeHref(href);

            String url = href.getData().getString("url");
            handleUrl(url); // handle the url here

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

答案 1 :(得分:7)

试试这个。

 @Override
        public boolean onCreateWindow(WebView view, boolean isDialog,
                boolean isUserGesture, Message resultMsg) {
            Logger.d(Constants.TAG, "onCreateWindow"+resultMsg);
            WebView targetWebView = new WebView(getActivity()); // pass a context
            targetWebView.setWebViewClient(new WebViewClient(){
                @Override
                public void onPageStarted(WebView view, String url,
                        Bitmap favicon) {
                    handleWebViewLinks(url); // you can get your target url here
                    super.onPageStarted(view, url, favicon);
                }
            });
            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(targetWebView);
            resultMsg.sendToTarget();
            return true;
        }

答案 2 :(得分:2)

不需要开始加载网页的albeee's解决方案的版本:

@override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
    Logger.d(Constants.TAG, "onCreateWindow"+resultMsg);
    WebView targetWebView = new WebView(getActivity()); // pass a context
    targetWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading (WebView view, String url) {                   handleWebViewLinks(url); // you can get your target url here
            return true; // return false if you want the load to continue
        }
    });
    WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
    transport.setWebView(targetWebView);
    resultMsg.sendToTarget();
    return true;
}