Intent and Intent Filter
总体介绍:
Intent Object,是在Sender和Receiver之间的传递媒介,说白了就是一个数据结构,Sender发出,Receiver接收,Receiver有以下几种类型,Activity, Service, Broadcast receiver. Intent是符合Android的设计思路的,程序被划分一个个的Activity, 各个Activity之间传递数据是需要一个载体的,那就是Intent. Intent并无任何巧妙之处。说白了,跟IPC有什么区别呢。
一个Intent包括两种信息,第一, Receiver感兴趣的东西。无容置疑。 第二,Android控制系统感兴趣的东西,Intent是通过Android系统转发的,要让Android读懂该Intent. 也很好理解。
一般如下:
模糊定位
Implicit intents就是指没有指定Component name的Intent(相对于Explicit intent). Android的设计鼓励通信的双方不需要知道彼此太多,因为有可能Sender往往不知道Receiver的具体名字。这个时候,是需要Android的模糊定位设计的,用于找到最合适的Receiver.这个勉强值得学习学习。
模糊定位的总体思路是:Receiver描述自己所能接受的Intent类型。当Android system派发Intent的时候,挨个比较Receiver的描述和所派发的Intent的内容,如果匹配,则成功。当找到多个匹配的时候,会提示用户选择一个,当找不到匹配的时候,会抛出异常
这里面涉及到一个概念叫Intent Filter,说白了就是Receiver用来描述自己所能接受的Intent类型。当Receiver定义了Intent Filter的时候,就能接受Implicit Intent了,如果没有定义任何Filter, 则只能接受Explicit intent. 模糊定位的过程就是匹配Intent和Intent Filter的过程。注意,Intent Filter只对Imlicit Intent有用,Explicit Intent完全不鸟这个流程,既然已经是精确定位了,则总是可以发送和接收的。
Android System在对Intent和Intent Filter进行匹配的时候,往往是比较一下三个数据:
1. Action
2. Data
3. Category.
Extra和Flags没鸟用。注意,不仅仅是比较Category, 我也误解过
既然是比较这三个数据,也就说Intent Filter可以设置这三个数据。例子:
<activity android:name="NoteEditor" android:theme="@android:style/Theme.Light"
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" />
<action android:name="com.android.notepad.action.EDIT_NOTE" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="vedio/mpeg" android:schema=”http”... />
<data android:mimeType="audio/mpeg" android:schema=”http”... />
</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>
总体介绍:
Intent Object,是在Sender和Receiver之间的传递媒介,说白了就是一个数据结构,Sender发出,Receiver接收,Receiver有以下几种类型,Activity, Service, Broadcast receiver. Intent是符合Android的设计思路的,程序被划分一个个的Activity, 各个Activity之间传递数据是需要一个载体的,那就是Intent. Intent并无任何巧妙之处。说白了,跟IPC有什么区别呢。
一个Intent包括两种信息,第一, Receiver感兴趣的东西。无容置疑。 第二,Android控制系统感兴趣的东西,Intent是通过Android系统转发的,要让Android读懂该Intent. 也很好理解。
一般如下:
1. Component name. 就是receiver.可选,一般是Activity名字
2. Action. 描述Receiver需要做的事情。重要。有一些系统预定义的Action, 比如ACTION_CALL, 初始化拨电话功能。也可以使用自定义的Action.这个应该是大多数情况吧。
3. Data.传输的数据
4. Category. receiver, 在不指定Component name的时候,可以指定Category来确定Receiver. 这个算作模糊考虑,可以方便的扩展Receiver, 不用指定具体的某个,而是指定大概的某种就可以。类似于Action. 有系统预定义的,也有用户自定义的
5. Extra. 也是数据,作用和Data一样,一般都是键值对,可以方便的传递小型数据,恐怕用得最多的吧
6. Flags. 标识位,总是用于对Intent的描述,给Android系统用的。所以值也往往是Android系统预定义好的。
恩,应该差不多了,作为一个传递载体,总是需要如下信息:
1. 谁发送。自动写入Intent内部,不需要操心
2. 发给谁。 Component name/Category
3. 做什么. Action
4. 数据. Data/Extra
5. 注意事项。Flags
模糊定位
Implicit intents就是指没有指定Component name的Intent(相对于Explicit intent). Android的设计鼓励通信的双方不需要知道彼此太多,因为有可能Sender往往不知道Receiver的具体名字。这个时候,是需要Android的模糊定位设计的,用于找到最合适的Receiver.这个勉强值得学习学习。
模糊定位的总体思路是:Receiver描述自己所能接受的Intent类型。当Android system派发Intent的时候,挨个比较Receiver的描述和所派发的Intent的内容,如果匹配,则成功。当找到多个匹配的时候,会提示用户选择一个,当找不到匹配的时候,会抛出异常
这里面涉及到一个概念叫Intent Filter,说白了就是Receiver用来描述自己所能接受的Intent类型。当Receiver定义了Intent Filter的时候,就能接受Implicit Intent了,如果没有定义任何Filter, 则只能接受Explicit intent. 模糊定位的过程就是匹配Intent和Intent Filter的过程。注意,Intent Filter只对Imlicit Intent有用,Explicit Intent完全不鸟这个流程,既然已经是精确定位了,则总是可以发送和接收的。
Android System在对Intent和Intent Filter进行匹配的时候,往往是比较一下三个数据:
1. Action
2. Data
3. Category.
Extra和Flags没鸟用。注意,不仅仅是比较Category, 我也误解过
既然是比较这三个数据,也就说Intent Filter可以设置这三个数据。例子:
<activity android:name="NoteEditor" android:theme="@android:style/Theme.Light"
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" />
<action android:name="com.android.notepad.action.EDIT_NOTE" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="vedio/mpeg" android:schema=”http”... />
<data android:mimeType="audio/mpeg" android:schema=”http”... />
</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>