Android如何直接鏈接到默認瀏覽器

原問題來自於CSDN問答頻道,更多解決方案見:http://ask.csdn.net/questions/1995

問題描述:

我在webview中加載了一個 website。當點擊一個鏈接"Full Site",我想開啓手機的默認瀏覽器,如何實現這個功能呢?目前它在web視圖中加載了完整的網站。

解決方案:

你需要在 WebView 對象上添加一個 WebViewClient

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
........

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.mysite.com")) {
           //Load the site into the default browser
             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
             startActivity(intent);
             return true;
        }
        // Load url into the webview
       return false;
    }
}


如果需要調整 if-statement語句。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章