您的位置:首页 > 移动开发 > Android开发

Android学习笔记 ---- 隐式Intent使用--打开Android默认浏览器

2018-03-04 10:20 393 查看
使用隐式Intent,在AndroidManifest.xml通过在<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action和category,我们不仅可以启动自己程序内的活动,还可以启动其他程序的活动,比如调用系统的浏览器来打开某网页 
  Intent intent = new Intent(Intent.ACTION_VIEW);    //为Intent设置Action属性
  intent.setData(Uri.parse("http://www.baidu.com")); //为Intent设置DATA属性
  startActivity(intent);
     Intent.ACTION_VIEW,这是一个Android系统内置的动作,其常量值为android.intent.action.VIEW。然后通过Uri.parse()方法,将一个网址字符串解析成一个Uri对象,再调用Intent的setData()方法将这个Uri对象传递进去。
 
     在AndroidManifest.xml中为Activity进行注册。
<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
<!-- 需要加上这句,不然会报错 -->
<category android:name="android.intent.category.BROWSABLE"/>
  <data android:scheme="http" /> <!--响应所有的http协议的Intent--></intent-filter>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: