您的位置:首页 > 其它

Intent学习笔记整理

2011-02-22 18:18 337 查看
1.Intent概念

根据开发文档上的描述——“Intent是对一个执行操作的抽象描述”。

个人理解:Intent像是一个媒介,或者说信使,提供组件相互调用的信息。

而在Intent中包含的信息包括对一次操作的动作、该动作涉及的数据、附加数据的描述。其中最重要的是前动作和动作对应的数据。

2.Intent属性

1>Action ,也就是对动作的描述。是个字符串,是对所将执行的动作的描述,在Intent类中定义了一些字符串常量作为标准动作。

标准的activity的Action可以自行查看文档(android developer网登陆不了的可以在这里查看

最常用的两个动作是ACTION_MAIN,ACTION_EDIT

此外,可以自己定义Action

2>Data,即动作要操作的数据,以URI的形式表示。譬如在联系人应用中,指向联系人1的URI可能为:content://contacts/1 (由content provider提供的数据类型是content) 。 结合Action和data可以基本表达出意图:

ACTION_VIEW
content://contacts/people/1
-- Display information about the person whose identifier is "1".

ACTION_DIAL
content://contacts/people/1
-- Display the phone dialer with the person filled in.

ACTION_VIEW
tel:123
-- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.

ACTION_DIAL
tel:123
-- Display the phone dialer with the given number filled in.

ACTION_EDIT
content://contacts/people/1
-- Edit information about the person whose identifier is "1".

ACTION_VIEW
content://contacts/people/
he-- Display a list of people, which t user can browse through.

注意对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为
content:
URIs

此外,还有一些其他属性:

3>category,被执行的动作的附加信息。如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。

其他的详细列表可以参考开发文档

4>type,显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

5>component,指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

6>extras,是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

3.Intent解析

在应用中Intent有两种不同的形式

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

简单说就是在构造Intent对象时就指定接收者

例如

Intent intent = new Intent(this, MyActivity.class);
intent.getExtras().putString("id", "1");
tartActivity(intent);


在这里,已经指定MyActivity即为接收者。

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

简单说就是,在构造Intent对象时,并不知道也不关心接收者是谁

对于直接Intent,Android不需要去做解析,因为目标组件已经很明确,Android需要解析的是那些间接Intent,通过解析,将 Intent映射给可以处理此Intent的Activity、BroadcastReceiver或Service。

如果Intent指明定了action,则目标组件的IntentFilter的action列表中就必须包含有这个action,否则不能匹配;

如果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。

如果Intent中的数据不是content: 类型的URI,而且Intent也没有明确指定它的type,将根据Intent中数据的scheme (比如 http: 或者mailto: ) 进行匹配。同上,Intent 的scheme必须出现在目标组件的scheme列表中。

如果Intent指定了一个或多个category,这些类别必须全部出现在组建的类别列表中。比如Intent中包含了两个类别:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目标组件必须至少包含这两个类别。

以一个图片来显示匹配过程(详见这里



实例:

下面以官方开发文档里的实例来分析。这是个记事本应用程序,允许用户可以浏览记事列表并点击查看详细信息。

AndroidManifest.xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.notepad">
<application android:icon="@drawable/app_notes"
android:label="@string/app_name">
<provider class=".NotePadProvider"
android:authorities="com.google.provider.NotePad" />
<activity class=".NotesList" android:label="@string/title_notes_list">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
</activity>
<activity class=".NoteEditor" android:label="@string/title_note">
<intent-filter android:label="@string/resolve_edit">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
</activity>
<activity class=".TitleEditor" android:label="@string/title_edit_title"
android:theme="@android:style/Theme.Dialog">
<intent-filter android:label="@string/resolve_title">
<action android:name="com.android.notepad.action.EDIT_TITLE" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
</activity>
</application>
</manifest>


这里面有三个activity。

第一个activity是主程序入口,根据intent的描述,这个activity有三个模块。

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>


这一部分是进入记事本应用的顶层:标准的MAIN action是主程序入口,而LAUNCHER category表明这个进入点应被列在应用程序开始处。

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data mimeType:name="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>


这些声明了activity对于记事条目目录所能做的操作,包括用户查看或编辑目录数据(通过VIEW与EDIT操作),或者选择一个条目返回给调用者(通过PICK动作)。此处显示指定了数据的类型为一个URI(
vnd.android.cursor.dir/vnd.google.note
)。此外还有DEFAULT分类(category),用于当组件名称没有直接指定时Context.startActivity方法来解析你的活动。

<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>


这一部分描述了返回给调用者用户选择的条目,而且不需要知道其来源。GET_CONTENT操作与PICK相似。不同的是调用者指定了所需的数据类型。

后面的部分可以自行查阅

参考:http://haric.javaeye.com/blog/291128

/article/4716176.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: