Intent对象主要用来在Android程序的Activity,Service和BroadcastReceiver这3大组件之间传输数据,

而针对这3大组件,有独立的Intent传输机制,分别如下:
1、Activity:通过将一个Intent对象传递给Context.startActivity()或Activity.startActivityForResult(),
启动一个活动或者使用一个已经存在的活动去做新的事情。
2、Service:通过将一个Intent对象传递给Content.startService(),初始化一个Service或者传递一个新的指令给正在运行的Service;
类似的,通过将一个Intent对象传递给ContentBindService(),可以建立调用组件和目标服务之间的连接
3、BroadcastReceiver:通过将一个Intent对象传递给任何广播方法
(如:Context.sendBroadcast(),Context.sendOrderedBroadcast(),Context.sendStickyBroadcast()等,可以传递到所有感兴趣的广播接收者)

注意:在每种传输机制下,Android程序会自动查找合适的Activity,Service或者BroadcastReceiver来响应Intent(意图),
如果有必要的话,初始化他们,这些消息系统之间没有重叠,即广播意图只会传递给广播接收者,而不会传递给活动或者服务,反之亦然。


Intent通过下面的属性来描述以上的某个意图:
action:用来表示意图的动作,如:查看、发邮件、打电话
category:用来表示动作的类别
data:用来表示与动作要操作的数据。如:查看 联系人
type:对data类型的描述
extras:附加信息,如:详细资料,一个文件,某事
component:目标组件(显示Intent)

显示Intent
指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context,Class)来指定)。
通过指定具体的组件类,通知应用启动对应的组件

隐式Intent
没有指定Component属性的Intent.这些Intent需要包含足够的信息,这样系统才能够根据这些信息,在所有的可用组件中,确定满足此Intent的组件。

对于显示的Intent,Android不需要去解析,因为目标组件已经很明确,
Android需要解析的是那些隐式的Intent,通过解析,将Intent映射给可以处理此Intent的Activity、IntentReceiver或者Service

Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有Intent-filter及其定义的Intent,最终找到匹配的Intent。
1、如果Intent指明了action,则目标组件的Intent-Filter的action列表中就必须包含有这个action,否则不能匹配。
2、如果Intent没有提供type,系统将从data中得到数据类型,和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。
3、如果Intent的数据不是content:类型的URI,而且Intent也没有明确指定它的type,将根据Intent中的数据的scheme(比如http;或者mailto:)进行匹配。
同上,Intent的scheme必须出现在目标组件的scheme列表中。
4、如果Intent指定了一个或多个category,这些类别必须全部出现在组件的类别列表中。比如Intent中包含两个类别:LAUNCH_CATEGORY和ALTERNATIVE_CATEORY,
解析得到的目标组件必须至少包含这两个类别。

只有<action>和<category>中的内容同时能够匹配上Intent中指定的action和category时,这个活动才能够响应该Intent


Android  Intent简介

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical">
 5 
 6     <Button
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:onClick="toMain"
10         android:text="显示Intent   启动自己"
11         android:textAllCaps="false" />
12 
13     <Button
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:onClick="setComponent"
17         android:text="启动别包下的activity"
18         android:textAllCaps="false" />
19 
20     <Button
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:onClick="action"
24         android:text="ActionViewActivity响应此action"
25         android:textAllCaps="false" />
26 
27     <Button
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:onClick="web"
31         android:text="浏览网页"
32         android:textAllCaps="false" />
33 
34     <Button
35         android:layout_width="wrap_content"
36         android:layout_height="wrap_content"
37         android:onClick="call1"
38         android:text="系统拨号界面"
39         android:textAllCaps="false" />
40 
41     <Button
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:onClick="call2"
45         android:text="拨号"
46         android:textAllCaps="false" />
47 
48 </LinearLayout>
activity_main.xml

相关文章: