Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to know target url when WebChromeClient#onCreateWindow is called

Tags:

android

When i tap a hyperlink with target="_blank" attributes, WebChromeClient#onCreateWindow is called but i cannot find a way to now what url the new window will open? host page url is the only thing i can know?

I want to change the app behavior according to the target url. any help is appreciated, thank you!

like image 409
Kurosawa Hiroyuki Avatar asked Aug 27 '12 12:08

Kurosawa Hiroyuki


2 Answers

SOLVED

i could get clicked url by calling like as follows

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
}
like image 191
Kurosawa Hiroyuki Avatar answered Oct 02 '22 01:10

Kurosawa Hiroyuki


Maybe I am late to the party but none of the above solution worked for me on Android 10. So here's how I did it:

@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;
}
like image 25
Mustansir Avatar answered Oct 02 '22 00:10

Mustansir