Android 单击 URL 打开默认浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2378800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:36:59  来源:igfitidea点击:

Clicking URLs opens default browser

androidurlandroid-webviewandroid-websettings

提问by JaVadid

I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView. But it's opening the default browser and loading the page there?

我在我的WebView. 现在我需要的是,当用户单击加载的页面上的链接时,它必须像普通浏览器一样工作并以相同的WebView. 但是它正在打开默认浏览器并在那里加载页面?

I have enabled JavaScript. But still it's not working. Have I forgotten something?

我已启用 JavaScript。但它仍然不起作用。我是不是忘记了什么?

回答by Dave Webb

If you're using a WebViewyou'll have to intercept the clicks yourself if you don't want the default Android behaviour.

如果您使用的是 a WebView,如果您不想要默认的 Android 行为,则必须自己拦截点击。

You can monitor events in a WebViewusing a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.

您可以WebView使用WebViewClient. 你想要的方法是shouldOverrideUrlLoading(). 这允许您在选择特定 URL 时执行自己的操作。

You set the WebViewClientof your WebViewusing the setWebViewClient()method.

您使用方法设置WebViewClientWebView的。setWebViewClient()

If you look at the WebViewsample in the SDKthere's an example which does just what you want. It's as simple as:

如果您查看WebViewSDK中的示例,有一个示例可以满足您的需求。这很简单:

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

回答by realgt

in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:

在某些情况下,如果您获得不触发 url 加载方法的重定向,您可能需要覆盖 onLoadResource。在这种情况下,我尝试了以下操作:

@Override
public void onLoadResource(WebView view, String url)
{
    if (url.equals("http://redirectexample.com"))
    {
        //do your own thing here
    }
    else
    {
        super.onLoadResource(view, url);
    }           
}

回答by abbas

Official documentationsays, click on a link in a WebView will launch application that handles URLs. You need to override this default behavior

官方文档说,单击 WebView 中的链接将启动处理 URL 的应用程序。您需要覆盖此默认行为

    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });

or if there is no conditional logic in the method simply do this

或者如果方法中没有条件逻辑,只需执行此操作

myWebView.setWebViewClient(new WebViewClient());

回答by Confuse

Add this 2 lines in your code -

在您的代码中添加这 2 行 -

mWebView.setWebChromeClient(new WebChromeClient()); 
mWebView.setWebViewClient(new WebViewClient());?

回答by Chris Stillwell

The method boolean shouldOverrideUrlLoading(WebView view, String url)was deprecated in API 24. If you are supporting new devices you should use boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).

该方法boolean shouldOverrideUrlLoading(WebView view, String url)在 API 24 中已弃用。如果您支持新设备,则应使用boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).

You can use both by doing something like this:

您可以通过执行以下操作来使用两者:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    newsItem.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }
    });
} else {
    newsItem.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
}

回答by Samuele Bolognini

Arulx Z's answer was exactly what I was looking for.

Arulx Z 的回答正是我想要的。

I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:

我正在编写一个带有 Navigation Drawer 和 recyclerview 和 webviews 的应用程序,无论点击超链接如何(因此不启动外部 Web 浏览器),都可以在应用程序内保持 Web 浏览。为此,放置以下两行代码就足够了:

mWebView.setWebChromeClient(new WebChromeClient()); mWebView.setWebViewClient(new WebViewClient());?

mWebView.setWebChromeClient(new WebChromeClient()); mWebView.setWebViewClient(new WebViewClient());?

exactly under your WebView statement.

完全在您的 WebView 声明下。

Here's a example of my implemented WebView code:

这是我实现的 WebView 代码的示例:

public class WebView1 extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
    wv.setWebViewClient(new WebViewClient());    //the lines of code added
    wv.setWebChromeClient(new WebChromeClient()); //same as above

    wv.loadUrl("http://www.google.com");
}}

this way, every link clicked in the website will load inside your WebView. (Using Android Studio 1.2.2 with all SDK's updated)

这样,在网站中点击的每个链接都会加载到您的 WebView 中。(使用 Android Studio 1.2.2 并更新所有 SDK)