In a WebView is there a way for shouldOverrideUrlLoading to determine if it is catching a redirect vs. a user clicking a link?

前端 未结 6 575
时光说笑
时光说笑 2021-02-01 06:06

I would like to allow redirects to happen naturally in the WebView and only catch a new url if it is a happening because a user clicked something.

相关标签:
6条回答
  • 2021-02-01 06:27

    In SDK 24. can get the result by the new shouldOverrideUrlLoading(WebView view, WebResourceRequest request),

    there is a method isRedirect() in WebResourceRequest which gets whether the request was a result of a server-side redirect.

    0 讨论(0)
  • 2021-02-01 06:35

    I had a similar problem. I needed to open a page in a WebView in my activity. However, when users clicked on a link in the page, I wanted it to be opened in a "new window," meaning the browser activity. This posed a problem if the page has a redirect. In order to accomplish this, I did the following:

    1. I only show the webview once the page has finished loading.
    2. I keep a boolean telling me when the page had finished loading. User interaction can only happen after a page had finished loading. otherwise it's a redirect and should be opened in my acitivty.

    Code sample:

    private boolean mLoaded = false;
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(mLoaded){ // open in new browser window only if page already finished
            try
            {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                activity.startActivity(intent);
            }
            catch (Exception e)
            {
                Logger.log(e);
            }
        }
        return true;
    }
    
    @Override
    public void onPageFinished(WebView webView, String url) {
            layoutHoldingTheWebView.setVisibility(View.VISIBLE);
            mLoaded = true;
        }
    }
    
    0 讨论(0)
  • 2021-02-01 06:39

    I just found something in that SDK that might work, WebView.getHitTestResult() Let me know if anyone sees any flaws in this method.

    Here is an example of how I am using it:

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(view.getHitTestResult().getType() > 0){
             // From a user click, handle it yourself.
             return true;
        } else {
             // Nothing clicked, assumed to be a redirect, let it redirect.
             return false;
        }
    }
    
    0 讨论(0)
  • 2021-02-01 06:41

    Adapted from Garrett's answer because I found it didn't quite work as written , gave a few errors etc. And the part about visibility is in my view not needed. I had the exact same issue as the OP and this is giving the behaviour I want:

    webView.setWebViewClient(new WebViewClient()
        {
            private boolean mLoaded = false;
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(mLoaded){ // open in new browser window only if page already finished
                    try
                    {
                        Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        view.getContext().startActivity(intent);
                    }
                    catch (Exception e)
                    {
                        Log.d("webview", e.getMessage());
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            @Override
            public void onPageFinished(WebView webView, String url)
            {
                mLoaded = true;
            }
        }
    
    0 讨论(0)
  • 2021-02-01 06:45

    Not directly, no.

    You can use a combination of WebViewClient.onPageStarted and WebView.getOriginalUrl to try to determine if the url being display is the original one requested, but this is after the WebView has decided to load the Url. This can help determine if a redirect has happened but still won't distinguish between user events and script events that do not cause redirects.

    If you control the content of the web content, you could also encode the user-clickable Urls in some identifiable way (eg. a proprietary scheme instead of http) and do any additional processing that you require if the criteria match in your shouldOverrideUrlLoading implementation.

    0 讨论(0)
  • 2021-02-01 06:47

    You can do as follow:

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if (webView.getUrl() != null && webView.getOriginalUrl() != null && webView.getOriginalUrl().equalsIgnoreCase(webView.getUrl()) {
            // Not Redirect
        } else {
            // Redirect
        }
    }
    
    0 讨论(0)
提交回复
热议问题