当我点击我的应用程序中的链接时,它们会在同一个webview中打开。 我希望它们在外部浏览器中打开。
我这样做了:
myWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } });
返回false使其在同一webview中加载,返回“true”使得单击链接时不会发生任何事情。
我看了其他问题,但似乎其他人都有完全相反的问题。 (他们想要在他们的应用中加载链接)
我究竟做错了什么?
在您的WebViewClient
@Override public boolean shouldOverrideUrlLoading(final WebView view, final String url){ if (loadUrlExternally){ Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); return true; //the webview will not load the URL } else { return false; //the webview will handle it } }
这样它就会像其他应用程序一样打开一个新的浏览器窗口。
这是一个更完整的答案。 注意:我从一个片段调用因此startActivity()之前的getActivity()
@Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) { //check if the url matched the url loaded via webview.loadUrl() if (checkMatchedLoadedURL(url)) { return false; } else { getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } /** * used to check if the loaded url matches the base url loaded by the fragment(mUrl) * @param loadedUrl * @return true if matches | false if doesn't or either url is null */ private boolean checkMatchedLoadedURL(String loadedUrl) { if (loadedUrl != null && mUrl != null) { // remove the tailing space if exisits int length = loadedUrl.length(); --length; char buff = loadedUrl.charAt(length); if (buff == '/') { loadedUrl = loadedUrl.substring(0, length); } // load the url in browser if not the OTHER_APPS_URL return mUrl.equalsIgnoreCase(loadedUrl); } return false; }