How To Open An URL In Android’s Web Browser
以下核心代码片断,将展现使用“android.content.Intent” 打开一个指定的 URL。
复制代码- [java] view plaincopy
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- Intent intent = new Intent(Intent.ACTION_VIEW,
- Uri.parse("http://www.tsingyuan.cn"));
- startActivity(intent);
- }
- });
|
再来一个例子:
How can I open a URL in Android's web browser from my application?
复制代码- [java] view plaincopy
- Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
- startActivity(browserIntent);
|
因为经常省略http://,所以以下代码也是经常用到:
复制代码- [java] view plaincopy
- if (!url.startsWith("http://") && !url.startsWith("https://"))
|