简体   繁体   中英

How to intercept link requests in a webView?

I have a WebView which contains a Facebook button, it opens a link to the site with a post already done. I want to know how to intercept the request and open it in the facebook application rather than on the browser. However, if Facebook apps is on the smartphone.

Thank you for your reply, and sorry for my English medium.

This can be achieved by setting the webviewclient eg.

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("http://blablablacatchurl")) {

            //we have intercepted the desired url call
            return true;
        }

        return super.shouldOverrideUrlLoading(view, url);
    }
});

If you want to see if there is a way to deep link to the facebook app, just package the url in an intent and fire it off and see what responds. If facebook doesn't catch it I don't think you can perform the second part of your request in any other way.

In case of not firing shouldOverrideUrlLoading method you could override onPageStarted or shouldInterceptRequest methods. They both have WebView as method parameter so you can redirect webview. but if you want to reach webview it must be final and it should run on different thread

@Override
public WebResourceResponse shouldInterceptRequest(final WebView view, WebResourceRequest request) {
    Log.i(TAG, "shouldInterceptRequest loading " + request.getMethod() + " " + request.getUrl().toString());
    String url = request.getUrl().toString();
    request.getRequestHeaders();
    if (url.contains("something important")){
        view.post(new Runnable() {
            @Override
            public void run() {
                if (view.canGoBack())
                    view.goBack();
                else
                    Log.e(TAG, "can NOT go back");
            }
        });
    }

    return super.shouldInterceptRequest(view, request);
}

您应该使用WebViewClient类并覆盖url加载。

webView.setWebViewClient(new MyWebClient());

    private class MyWebClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            //return super.shouldOverrideUrlLoading(view, url);
            Uri uri;

            try {
                uri = Uri.parse(url);
            } catch (NullPointerException e) {
                // let Android deal with this
                return true;
            }

            String host = uri.getHost(); //Host is null when user clicked on email, phone number, ...

            if (host != null && host.equals("stackoverflow.com")) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            else {
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs or anything else (email, phone number, ...)
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM