In my Android app I have a WebView element with a custom WebViewClient. It shows an error page via onReceivedError(), and it returns false for shouldOverrideUrlLoading().

I would expect the WebView now to open links in the external browser, not in the WebView itself, as it is explained in many other posts on SO. However, this does not work. It still opens every link inside the WebView.

Has it maybe something to do with the fact that I don't load the content via loadUrl() but via loadData() and pass an html-string?

有帮助吗?

解决方案

You must use an intent to launch the url in the external browser. Method shouldOverrideUrlLoading() is a good candidate:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Context context = view.getContext();
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

    // Launch default browser
    context.startActivity(browserIntent);

    return true;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top