WebView链接点击打开默认浏览器

现在我有一个应用程序加载一个web视图,所有的点击都保存在应用程序内。 我想要做的是当一个特定的链接,例如http://www.google.com点击应用程序中打开默认浏览器。 如果有人有一些想法,请让我知道!

今天我必须做同样的事情,我已经find了一个非常有用的答案StackOverflow,我想在这里分享,以防别人需要它。

来源 (来自sven )

webView.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url != null && url.startsWith("http://")) { view.getContext().startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } else { return false; } } }); 
 WebView webview = (WebView) findViewById(R.id.webview); webview.loadUrl(http://www.playbuzz.org); 

你不必包含这个代码// webview.setWebViewClient(new WebViewClient()); 相反,你需要使用下面的d代码

 webview.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { String url2="http://www.playbuzz.org/"; // all links with in ur site will be open inside the webview //links that start ur domain example(http://www.example.com/) if (url != null && url.startsWith(url2)){ return false; } // all links that points outside the site will be open in a normal android browser else { view.getContext().startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } } }); 

你可以使用意图这个:

 Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url")); startActivity(browserIntent); 

您只需添加以下行

 yourWebViewName.setWebViewClient(new WebViewClient()); 

检查这个官方文件。

你可以使用这个意图:

 Uri uriUrl = Uri.parse("http://www.google.com/"); Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(launchBrowser);