Intent filter是在Android的主配置文件AndroidManifest.xml中注册,主要用来指明Activity, Service, Broadcast reciver这三个组件可以响应哪些隐式intents。每个组件可以有一个或者多个intent filter。
(android四大组件分别为activity、service、content provider、broadcast receiver。)
Intent filter有三个部分构成,分别是action(动作),data(数据)和category(类别)。下面通过代码分析每个部分的功能。
<intent-filter android:label="@string/Asen's blog">
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.ALTERNATIVE" />
<data android:mimeType="video/mpeg" android:scheme="http".../>
<data android:mimeType="audio/mpeg" android:content="com.example.project:200/folder/subfolder/etc"/>
</intent-filter>
每个action, category, data都是一行,如果有多个就写多行。
另外,”android.intent.category.DEFAULT”这个category是用来指明组件是否可以接收到隐式Intents,所以说除了程序入口点这个filter不用包含DEFAULT category外,其余所有intent filter都要有这个category。
action
使用 android:name 特性来指定对响应的动作名。动作名必须是独一无二的字符串,所以,一个好的习惯是使用基于 Java 包的命名方式的命名系统。
category
使用 android:category 属性用来指定在什么样的环境下动作才被响应。每个 Intent Filter 标签可以包含多个 category 标签。你可以指定自定义的种类或使用 Android 提供的标准值
data
data 标签允许你指定组件能作用的数据的匹配;如果你的组件能处理多个的话,你可以包含多个条件。你可以使用下面属性的任意组合来指定组件支持的数据
data有两部分构成,一个是数据类型,另一个是URI。每个URI包括四个属性参数(scheme,host, port, path),形如:scheme://host:port/path举个例子 content://com.examplproject:e.200/folder/subfolder/etc 这个列子中scheme是content,host是com.examplproject,port是200,path是folder/subfolder/etcIntent filter和Intent相互配合,实现了Android系统四大组件之间的信使功能。