Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle URL from onCreateWindow WebView

Can I get URL from calling onCreateWindow on WebView?

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

}
like image 373
user1021984 Avatar asked Mar 11 '12 11:03

user1021984


3 Answers

You can get clicked URL by using this code.

@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);
}
like image 163
BeyazBaron Avatar answered Nov 03 '22 12:11

BeyazBaron


A version of albeee's solution that doesn't need to start loading the web page:

@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;
}
like image 23
Brian Batchelder Avatar answered Nov 03 '22 13:11

Brian Batchelder


Try this.

@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;
}
like image 31
albeee Avatar answered Nov 03 '22 13:11

albeee