在浏览器中从WebView打开URL

时间:2015-04-14 00:23:59

标签: android url android-webview android-browser

我需要在手机的默认浏览器中打开网址(而不是在应用程序的WebView中)。以下是我的代码,但它会在WebView中启动网址。

如何在默认浏览器中打开网址?

Activity2.java

public class Activity2 extends Activity {
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_webview);

      webview=(WebView)findViewById(R.id.webView1);
      webview.setWebViewClient(new MyWebViewClient());
      openURL();
    }

    /** Opens the URL in a browser */
    private void openURL() {
      webview.loadUrl("http://www.XX.org");
      webview.requestFocus();
    }
}

1 个答案:

答案 0 :(得分:1)

不需要webview,如果您计划在浏览器中打开网址,请使用以下代码

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.XX.org"));
startActivity(i);

如果您想从代码中打开Facebook应用程序:

try {       
    String uri = "facebook://facebook.com/inbox";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    startActivity(intent);
} catch (ActivityNotFoundException e) {
}
相关问题