webview loadUrl在浏览器中打开url [英] webview loadUrl opens the url in browser

查看:353
本文介绍了webview loadUrl在浏览器中打开url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在webview中打开网址,但它正在外部浏览器中打开网址。

I am trying to open the url in webview, but it is opening the url in external browser.

我的代码是

public class MainActivity extends Activity
{

    private WebView webView;

    String URL = "https://login.salesforce.com/services/oauth2/authorize?response_type=token&display=touch&client_id=3MVG9Y6d_Btp4xp7w.6oGLerlRztTTUKMEL8QWHvuB5miUgdJzQ0HgnMB7dhm1mTiluRv4ud9cZYeFcAz7hXP&redirect_uri=https%3A%2F%2Flogin.salesforce.com%2Fservices%2Foauth2%2Fsuccess";
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);



        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.getSettings().setDomStorageEnabled(true);
        webView.loadUrl(URL);
        //webView.loadUrl("https://www.google.co.in/");
        webView.setWebViewClient(new MyWebViewClient());
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(url)) {
                // This is your web site, so do not override; let the WebView to load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);

            // this will ignore the Ssl error and will go forward to your site
            handler.proceed();
        }
    }

}

如果网址是https:www.google.com,然后网址仅在网页浏览中打开。我用Google搜索找到解决方案,但我无法找到它。如何解决这个问题?

If the url is https:www.google.com then the url is opening in webview only. I have googled hard to find the solution but I am unable to find it. How to resolve the issue?

推荐答案

问题在你的

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals(url)) {
            // This is your web site, so do not override; let the WebView to load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }

尝试将URL加载为不同的意图,因此您的默认浏览器会启动in。
你真正想要检查什么

its trying to load the URL as a different intent, hence your default browser kicks in. what do you really want to check with


Uri.parse(url).getHost()。equals(url) )

Uri.parse(url).getHost().equals(url)

尝试将日志用于支票,你会发现错误。

try putting logs for the check you will figure out what wrong.

这篇关于webview loadUrl在浏览器中打开url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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