Android中Intent主要分为显式Intent和隐式Intent,今天会主要讲隐式Intent
一、显式Intent
最常用的就是下面这种形式
Intent intent = new Intent(FirstActivity.this, Second.class);
startActivity(intent);
二、隐式Intent
这种Intent方式主要是通过action和category来由系统分析该启动哪一个活动
1、指定action
AndroidManifest.xml中
<activity android:name=".WelcomeActivity">
<intent-filter>
<action android:name="com.bingjian.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Intent intent = new Intent("com.bingjian.ACTION_START");
startActivity(intent);
只有指定的action和category同时匹配才会响应该活动,上面的代码在启动时之所以没有指定category是因为android.intent.category.DEFAULT是一直默认的category,会在startActivity时默认杯添加到intent中。
2、指定action和category
<activity android:name=".WelcomeActivity">
<intent-filter>
<action android:name="com.bingjian.ACTION_START" />
<category android:name="com.bingjian.MY_CATEGORY" />
</intent-filter>
</activity>
Intent intent = new Intent("com.bingjian.ACTION_START");
intent.addCategory("com.bingjian.MY_CATEGORY");
startActivity(intent);
3、启动其他程序的活动
Intent intent = new Intent("Intent.ACTION_VIEW");
intent.setData(Uri.parse("http://www.qq.com"));
startActivity(intent);
上面的代码会调用浏览器打开http://www.qq.com
4、在标签中配置data标签
data标签中主要可以配置以下内容
- android:scheme 用来指定数据的协议部分
- android:host 用于指定数据的主机名部分
- android:port 指定数据的端口
- android:path 指定主机名和端口之后的部分
- android:mimeType 指定可以处理的数据类型,可以使用通配符
常用的数据协议
- http协议
- geo 地理位置
- tel 拨打电话