在默认浏览器中打开URL

时间:2014-11-04 14:32:34

标签: java android webview

我有一个webview,我加载一个网址,当我点击webview我想在默认浏览器中打开链接。我试图设置WebViewClient和override shouldOverrideUrlLoading(WebView view, String url)方法

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.e("BaseFragment", "shouldOverrideUrlLoading  " + url);
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(i);
        return false;
    }

但是webview没有调用它。还有其他想法吗?

BaseFragment是一个抽象类,我有以下WebViewClient:

protected WebViewClient mAddWebViewClient = new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        float scale = addTopWebView.getHeight() / 170f;
        addTopWebView.setInitialScale((int) (scale * 100f));
        String js = "javascript:(function(){"
                + "document.getElementsByTagName('div')[0].style.height = 'auto';"
                + "document.getElementsByTagName('div')[0].style.width = 'auto';"
                + "})()";
        view.loadUrl(js);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.e("BaseFragment", "shouldOverrideUrlLoading  " + url);
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(i);
        return true;
    }

};

扩展此BaseFragment的其他片段有一些webview,在onViewCreated方法中我设置了webviewclient:

    myWebView.getSettings().setJavaScriptEnabled(true);
    addTopWebView = myWebView;
    myWebView.setWebViewClient(mAddWebViewClient);

1 个答案:

答案 0 :(得分:1)

我认为你应该为外部浏览器返回true。

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Log.e("BaseFragment", "shouldOverrideUrlLoading  " + url);
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(i);
    return true;
}

如果要在自定义网页浏览中显示,请覆盖并将返回值设置为false。