在WebView中,shouldOverrideUrlLoading是否可以确定它是否在捕获重定向,而不是用户单击链接? [英] In a WebView is there a way for shouldOverrideUrlLoading to determine if it is catching a redirect vs. a user clicking a link?

查看:71
本文介绍了在WebView中,shouldOverrideUrlLoading是否可以确定它是否在捕获重定向,而不是用户单击链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望允许重定向在WebView中自然发生,并且仅在由于用户单击某些内容而发生新URL时才捕获新URL.

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.

推荐答案

我遇到了类似的问题.我需要在活动中在WebView中打开一个页面.但是,当用户单击页面中的链接时,我希望在新窗口"中打开它,这意味着浏览器处于活动状态.如果页面具有重定向,则会造成问题.为了做到这一点,我做了以下工作:

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. 仅在页面加载完成后才显示Web视图.
  2. 当页面加载完成时,我会用布尔值告诉我.用户交互只能在页面完成加载后发生.否则,它是一个重定向,应该在我的活动中打开.

代码示例:

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;
    }
}

这篇关于在WebView中,shouldOverrideUrlLoading是否可以确定它是否在捕获重定向,而不是用户单击链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆