`

Android开发--Intent-filter属性详解

 
阅读更多

如果一个 Intent 请求在一片数据上执行一个动作, Android 如何知道哪个应用程序(和组件)能用来响应这个请求呢? Intent Filter就是 用来注册 Activity 、 Service 和 Broadcast Receiver 具有能在某种数据上执行一个动作的能力。

使用 Intent Filter ,应用程序组件告诉 Android ,它们能为其它程序的组件的动作请求提供服务,包括同一个程序的组件、本地的或第三方的应用程序。

为了注册一个应用程序组件为 Intent 处理者,在组件的 manifest 节点添加一个 intent-filter 标签。

在 Intent Filter 节点里使用下面的标签(关联属性),你能指定组件支持的动作、种类和数据:
1.动作测试
<intent-filter>元素中可以包括子元素<action>,比如:
view source print ?
  1. 1. < intent-filter >
  2. 2. < action android:name="com.example.project.SHOW_CURRENT" />
  3. 3. < action android:name="com.example.project.SHOW_RECENT" />
  4. 4. < action android:name="com.example.project.SHOW_PENDING" />
  5. 5. </ intent-filter >
一条<intent-filter>元素至少应该包含一个<action>,否则任何Intent请求都不能和该<intent-filter>匹配。如果Intent请求的Action和<intent-filter>中个某一条<action>匹配,那么该Intent就通过了这条<intent-filter>的动作测试。如果Intent请求或<intent-filter>中没有说明具体的Action类型,那么会出现下面两种情况。
(1) 如果<intent-filter>中没有包含任何Action类型,那么无论什么Intent请求都无法和这条<intent-filter>匹配;
(2) 反之,如果Intent请求中没有设定Action类型,那么只要<intent-filter>中包含有Action类型,这个Intent请求就将顺利地通过<intent-filter>的行为测试。
2.类别测试
<intent-filter>元素可以包含<category>子元素,比如:
view source print ?
  1. 1. < intent-filter . . . >
  2. 2. < category android:name="android.Intent.Category.DEFAULT" />
  3. 3. < category android:name="android.Intent.Category.BROWSABLE" />
  4. 4. </ intent-filter >
只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请求通过测试,IntentFilter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没有设置类别的Intent请求。
3.数据测试
数据在<intent-filter>中的描述如下:
view source print ?
  1. 1. < intent-filter . . . >
  2. 2. < data android:type="video/mpeg" android:scheme="http" . . . />
  3. 3. < data android:type="audio/mpeg" android:scheme="http" . . . />
  4. 4. </ intent-filter >
<data>元素指定了希望接受的Intent请求的数据URI和数据类型,URI被分成三部分来进行匹配:scheme、 authority和path。其中,用setData()设定的Inteat请求的URI数据类型和scheme必须与IntentFilter中所指定的一致。若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。

❑ action
使用 android:name 特性来指定对响应的动作名。动作名必须是独一无二的字符串,所以,一个好的习惯是使用基于 Java 包的命名方式的命名系统。

❑ category
使用 android:category 属性用来指定在什么样的环境下动作才被响应。每个 Intent Filter 标签可以包含多个 category 标签。你可以指定自定义的种类或使用 Android 提供的标准值,如下所示:

❑ ALTERNATIVE
你将在这章的后面所看到的,一个 Intent Filter 的用途是使用动作来帮忙填入上下文菜单。 ALTERNATIVE 种类指定,在某种数据类型的项目上可以替代默认执行的动作。例如,一个联系人的默认动作时浏览它,替代的可能是去编辑或删除它。

❑ SELECTED_ALTERNATIVE
与 ALTERNATIVE 类似,但 ALTERNATIVE 总是使用下面所述的 Intent 解析来指向单一的动作。SELECTED_ALTERNATIVE在需要一个可能性列表时使用。

❑ BROWSABLE
指定在浏览器中的动作。当 Intent 在浏览器中被引发,都会被指定成 BROWSABLE 种类。

❑ DEFAULT
设置这个种类来让组件成为 Intent Filter 中定义的 data 的默认动作。这对使用显式 Intent 启动的 Activity 来说也是必要的。

❑ GADGET
通过设置 GADGET 种类,你可以指定这个 Activity 可以嵌入到其他的 Activity 来允许。

❑ HOME
HOME Activity 是设备启动(登陆屏幕)时显示的第一个 Activity 。通过指定 Intent Filter 为 HOME 种类而不指定动作的话,你正在将其设为本地 home 画面的替代。

❑ LAUNCHER
使用这个种类来让一个 Activity 作为应用程序的启动项。

❑ data
data 标签允许你指定组件能作用的数据的匹配;如果你的组件能处理多个的话,你可以包含多个条件。你可以使用下面属性的任意组合来指定组件支持的数据:

❑ android:host
指定一个有效的主机名(例如, com.google )。

❑ android:mimetype
允许你设定组件能处理的数据类型。例如,<type android:value=”vnd.android.cursor.dir/*”/>能匹配任何 Android 游标。

❑ android:path
有效地 URI 路径值(例如, /transport/boats/ )。

❑ android:port
特定主机上的有效端口。

❑ android:scheme
需要一个特殊的图示(例如, content 或 http )。

接下来的代码片段显示了如何配置 Activity 的 Intent Filter ,使其以在特定数据下的默认的或可替代的动作的身份来执行 SHOW_DAMAGE动作。(创建地震内容将在下一章节。)

Java代码
  
  1. 1. <activity android:name=".EarthquakeDamageViewer" 
  2.    2. 
  3.    3. android:label="View Damage"> 
  4.    4. 
  5.    5. <intent-filter> 
  6.    6. 
  7.    7. <action 
  8.    8. 
  9.    9. android:name="com.paad.earthquake.intent.action.SHOW_DAMAGE"> 
  10.   10. 
  11.   11. </action> 
  12.   12. 
  13.   13. <category android:name="android.intent.category.DEFAULT"/> 
  14.   14. 
  15.   15. <category 
  16.   16. 
  17.   17. android:name="android.intent.category.ALTERNATIVE_SELECTED" 
  18.   18. 
  19.   19. /> 
  20.   20. 
  21.   21. <data android:mimeType="vnd.earthquake.cursor.item/*"/> 
  22.   22. 
  23.   23. </intent-filter> 
  24.   24. 
  25.   25. </activity> 
  26. <activity android:name=".EarthquakeDamageViewer"
  27. android:label="View Damage">
  28. <intent-filter>
  29. <action
  30. android:name="com.paad.earthquake.intent.action.SHOW_DAMAGE">
  31. </action>
  32. <category android:name="android.intent.category.DEFAULT"/>
  33. <category
  34. android:name="android.intent.category.ALTERNATIVE_SELECTED"
  35. />
  36. <data android:mimeType="vnd.earthquake.cursor.item/*"/>
  37. </intent-filter>
  38. </activity>
分享到:
评论

相关推荐

    Intent filter 关于Action、Category属性详解源码

    Intent filter 关于Action、Category属性详解源码 对应的博客文章链接: http://blog.csdn.net/a13429921973/article/details/9271973

    Android_Intent和Intent_Filter详解

    Android_Intent和Intent_Filter详解

    Android Intent和Intent Filter详解

    Intents and Intent Filters  三种应用程序基本组件——activity, ... 在上述三种情况下, android系统会自己找到合适的activity, service, 或者 broadcast receivers来响应intent. 三者的intent相互独立互不干扰.

    详解Android中Intent对象与Intent Filter过滤匹配过程

    如果对Intent不是特别了解,可以参见博文《详解Android中Intent的使用方法》,该文对本文要使用的action、category以及data都进行了详细介绍。如果想了解在开发中常见Intent的使用,可以参见《Android中Intent习惯...

    Android Intent 、intent-filter详解

    参考...,讲解的很好 1.什么是Intent(定义) Intent这个单词的意思就是”意图,目的,意向”,Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件。 个

    Android Intent 组件通信与广播消息

    Android组件通信与广播消息,Intent与Intent-filter详解

    Android开发应用实战详解源代码

    2.2.2 intent和intent filter 2.2.3 service介绍 2.2.4 broadcastintentreceiver 2.2.5 contentprovider 2.3 android应用项目文件组成 2.3.1 androidmanifest.xml文件 2.3.2 src目录 2.3.3 常量的定义文件 2.4 程序...

    Android中Intent习惯用法

    如果对Intent Filter不是特别了解,可以参见《详解Android中Intent对象与Intent Filter过滤匹配过程》。 本文着重讲一下Android中一些常见的Intent的习惯用法,比如如何通过Intent发送短信、发送邮件、启动摄像机...

    Android入门到精通源代码.

    7.3.2 Intent Filter 7.4 设置Activity许可 7.5 应用实例详解:电话拨号程序 7.5.1 实例分析 7.5.2 实例实现 第8章 Android中的后台服务Service 8.1 Service的作用 8.2 Service的实现 8.2.1 创建Service 8.2.2 启动...

    Android 4.X手机/平板电脑程序设计入门、应用到精通_源代

     11个Intent专门单元,包括建立Tab卷标页、传送和回传数据、Intent Filter…,让您完整学会Intent的用法; 4大类Android程序完全详解,包括Activity、Service.Content provider和Broadcast receiver,以及App ...

    Android 中IntentFilter的匹配规则实例详解

    如果要成功匹配该intent-filter我们需要完全匹配该intent-filter中的 action、category、data。 示例: &lt;activity android:name=.MainActivity&gt; &lt;intent&gt; &lt;action android:name=com.blueberry.action&gt;&lt;/action&gt; &...

    Android中Image的简单实例详解

    在多媒体应用中,Image是最基础的功能...该应用含有一个Intent-Filter。通过使用 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent)就可以启动Camera应用了。 二、Ima

    Android实例代码

    5.2、Intent的属性及intent-filter配置:Component属性; Action、Category属性与intent-filter配置; Data、Type属性与intent-filter配置; Extra属性; 5.3、使用Intent创建Tab页面: 第6章、Android应用的资源 ...

    疯狂Android讲义(第2版)源代码 第6章~第9章

    5.2、Intent的属性及intent-filter配置:Component属性; Action、Category属性与intent-filter配置; Data、Type属性与intent-filter配置; Extra属性; 5.3、使用Intent创建Tab页面: 第6章、Android应用的资源 ...

    疯狂Android讲义源码

     5.2 Intent的属性及intent-filter  配置 198  5.2.1 Component属性 198  5.2.2 Action、Category属性与  intent-filter配置 200  5.2.3 指定Action、Category调用  系统Activity 204  5.2.4 Data、Type属性...

    疯狂Android讲义.part2

    5.2 Intent的属性及intent-filter 配置 198 5.2.1 Component属性 198 5.2.2 Action、Category属性与 intent-filter配置 200 5.2.3 指定Action、Category调用 系统Activity 204 5.2.4 Data、Type属性与intent-filter ...

    疯狂Android讲义.part1

    5.2 Intent的属性及intent-filter 配置 198 5.2.1 Component属性 198 5.2.2 Action、Category属性与 intent-filter配置 200 5.2.3 指定Action、Category调用 系统Activity 204 5.2.4 Data、Type属性与intent-filter ...

Global site tag (gtag.js) - Google Analytics