15

当我点击带有 , 的超链接时target="_blank" attributesWebChromeClient#onCreateWindow会调用但我现在找不到新窗口将打开什么网址的方法?主机页面 url 是我唯一能知道的吗?

我想根据目标 url 更改应用程序行为。任何帮助表示赞赏,谢谢!

4

4 回答 4

4

解决了

我可以通过如下调用来获得点击的网址

public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
    WebView.HitTestResult result = view.getHitTestResult();
    int type = result.getType();
    String data = result.getExtra();
    // do something
}
于 2012-08-31T01:15:34.730 回答
2

也许我迟到了,但上述解决方案都不适用于 Android 10。所以我是这样做的:

@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
    WebView newWebView = new WebView(view.getContext());
    WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
    transport.setWebView(newWebView);
    resultMsg.sendToTarget();
    newWebView.setWebViewClient(new WebViewClient() {
        @SuppressLint("NewApi")
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return shouldOverrideUrlLoading(view, request.getUrl().toString());
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView View, String url) {
            if (url != null && !url.isEmpty()) {
               // HERE IS URL WHICH YOU WANT
            }
            return false;
        }
    });
    return true;
}
于 2021-01-23T06:51:54.410 回答
1

我使用以下代码:

public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
    Message href = view.getHandler().obtainMessage();
    view.requestFocusNodeHref(href);           
    var url = href.getData()?.getString("url");

    // rest of your code
}
于 2019-10-04T17:30:46.773 回答
1

以下片段将完成工作。

@Override
    public boolean onCreateWindow (WebView view,boolean isDialog, boolean isUserGesture, Message resultMsg){
        //The default implementation
        //return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
        //This way you can get the URL
        String url = null;
        if (webView.getHitTestResult().getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE
               || webView.getHitTestResult().getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
            url = webView.getHitTestResult().getExtra();
        }
        return true;
    }

资源

于 2021-01-20T06:01:09.800 回答