主题 : 【代码】How To Open An URL In Android’s Web Browser 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
贡献值: 0 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-08-22 14:11

 【代码】How To Open An URL In Android’s Web Browser

How To Open An URL In Android’s Web Browser
以下核心代码片断,将展现使用“android.content.Intent” 打开一个指定的 URL。

复制代码
  1. [java] view plaincopy
  2. button.setOnClickListener(new OnClickListener() {
  3. @Override
  4. public void onClick(View arg0) {
  5. Intent intent = new Intent(Intent.ACTION_VIEW,
  6. Uri.parse("http://www.tsingyuan.cn"));
  7. startActivity(intent);
  8. }
  9. });


再来一个例子:
How can I open a URL in Android's web browser from my application?

复制代码
  1. [java] view plaincopy
  2. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
  3. startActivity(browserIntent);


因为经常省略http://,所以以下代码也是经常用到:
复制代码
  1. [java] view plaincopy
  2. if (!url.startsWith("http://") && !url.startsWith("https://"))
欢迎加入android开发交流群,群号是:314230976