标签:
l Intent协助应用间的交互与通讯
Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述、Android则根据此Intent的描述,负责找到对应的组建,将Intent传递给调用的组建,并完成组建的调用
l Intent可以激活Android应用的三种类型的核心组建:Activity、Service、BroadcastReceiver
l 可划分为:显示Intent【目标唯一】和隐士Intent【根据目标intentfilter进行筛选】
Intent的使用:
² 启动Activity:[显示]
Intent intent=new Intent(this,NewsDetailActivity.class);
super.startActivity(intent);
等价于
ComponentName comp=new ComponentName(this,NewsDetailActivity.class);
Intent intent=new Intent();
intent.setComponent(comp);
super.startActivity(intent);
l 在不明确设置激活对象的前提下寻找最匹配的组件
l Android系统会根据隐士Intent的动作(Action)、类别(Category)、数据(URI和数据类型)找到最合适的组件来处理这个意图
l Intent解析戒指通过查找AndroidMainfest.xml中的IntentFilter,最终找到匹配的Intentde action、type、category这三个属性来进行判断的
Intent-filter
l Action:可以有多个,程序中只要有一个匹配就行【必有】
l Category:必须至少有一个类别,用startActivity启动【必有】
l Data:路径匹配【协议scheme://主机名:端口号/路径】、数据类型匹配mimeType。
² 启动Activity:[隐式]
(1)在java代码中设置隐士对象
Intent intent=new Intent();
intent.setAction("show_details");//intent.ACTION_VIEW
intent.addCategory("detail");
// intent.setData(Uri.parse("http://www.jereh.com"));
// intent.setType("image/*");
super.startActivity(intent);
(2)AndroidMainfest.xml 相应的<activity>中进行配置
<intent-filter >
<action android:name="show_details"/>
<action android:name="android.intent.action.VIEW"/>
<!-- 隐士Intent必须保留 DEFAULT-->
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="detail"/>
<!-- <data android:mimeType="image/*"/>
<data android:scheme="http" android:host="www.jereh.com"/> -->
</intent-filter>
注意:在配置隐式intent的fitler时,必须包含默认的DEFAULT category属性
<category android:name="android.intent.category.DEFAULT"/>
一旦设置了data属性,基本上也就确定了一个Activity。
显示启动与隐式启动的区别:
匹配的Activity可以是应用程序本身的,也可以是Android系统内置的,还可以是第三方应用程序提供的。这种方式更加强调了Android应用程序中组建的可复用性。
标签:
原文地址:http://www.cnblogs.com/lcs1991/p/4605096.html