应用程序的组件为了告诉Android自己能响应、处理哪些隐式Intent请求,可以声明一个甚至多个Intent Filter。每个Intent Filter描述该组件所能响应Intent请求的能力——组件希望接收什么类型的请求行为,什么类型的请求数据。比如之前请求网页浏览器这个例子中,网页浏览器程序的Intent Filter就应该声明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及与之相关的请求数据是网页地址URI格式。如何为组件声明自己的Intent Filter? 常见的方法是在AndroidManifest.xml文件中用属性<Intent-Filter>描述组件的Intent Filter。
前面我们提到,隐式Intent(Explicit Intents)和Intent Filter(Implicit Intents)进行比较时的三要素是Intent的动作、数据以及类别。实际上,一个隐式Intent请求要能够传递给目标组件,必要通过这三个方面的检查。如果任何一方面不匹配,Android都不会将该隐式Intent传递给目标组件。接下来我们讲解这三方面检查的具体规则。
1.动作测试
<intent-filter>元素中可以包括子元素<action>,比如:
<intent-filter>
<action android:name=”com.example.project.SHOW_CURRENT” />
<action android:name=”com.example.project.SHOW_RECENT” />
<action android:name=”com.example.project.SHOW_PENDING” />
</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>子元素,比如:
<intent-filter . . . >
<category android:name=”android.Intent.Category.DEFAULT” />
<category android:name=”android.Intent.Category.BROWSABLE” />
</intent-filter>
只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请求通过测试,IntentFilter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没有设置类别的Intent请求。
3.数据测试
数据在<intent-filter>中的描述如下:
<intent-filter . . . >
<data android:type=”video/mpeg” android:scheme=”http” . . . />
<data android:type=”audio/mpeg” android:scheme=”http” . . . />
</intent-filter>
<data>元素指定了希望接受的Intent请求的数据URI和数据类型,URI被分成三部分来进行匹配:scheme、 authority和path。其中,用setData()设定的Inteat请求的URI数据类型和scheme必须与IntentFilter中所指定的一致。若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。
4.简单例子说明
讲解完Intent基本概念之后,接下来我们就使用Intent激活Android自带的电话拨号程序,通过这个实例你会发现,使用Intent并不像其概念描述得那样难。最终创建Intent的代码如下所示。
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));
创建好Intent之后,你就可以通过它告诉Android希望启动新的Activity了。
startActivity(i);
Activity启动后显示界面如下:
5.总结说明
这篇文章是我刚开始学习Android时看到的,当时理解的不是很深入,现在再回头看这篇文章总结的很详细,在这里与大家分享
前面我们提到,隐式Intent(Explicit Intents)和Intent Filter(Implicit Intents)进行比较时的三要素是Intent的动作、数据以及类别。实际上,一个隐式Intent请求要能够传递给目标组件,必要通过这三个方面的检查。如果任何一方面不匹配,Android都不会将该隐式Intent传递给目标组件。接下来我们讲解这三方面检查的具体规则。
1.动作测试
<intent-filter>元素中可以包括子元素<action>,比如:
<intent-filter>
<action android:name=”com.example.project.SHOW_CURRENT” />
<action android:name=”com.example.project.SHOW_RECENT” />
<action android:name=”com.example.project.SHOW_PENDING” />
</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>子元素,比如:
<intent-filter . . . >
<category android:name=”android.Intent.Category.DEFAULT” />
<category android:name=”android.Intent.Category.BROWSABLE” />
</intent-filter>
只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请求通过测试,IntentFilter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没有设置类别的Intent请求。
3.数据测试
数据在<intent-filter>中的描述如下:
<intent-filter . . . >
<data android:type=”video/mpeg” android:scheme=”http” . . . />
<data android:type=”audio/mpeg” android:scheme=”http” . . . />
</intent-filter>
<data>元素指定了希望接受的Intent请求的数据URI和数据类型,URI被分成三部分来进行匹配:scheme、 authority和path。其中,用setData()设定的Inteat请求的URI数据类型和scheme必须与IntentFilter中所指定的一致。若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。
4.简单例子说明
讲解完Intent基本概念之后,接下来我们就使用Intent激活Android自带的电话拨号程序,通过这个实例你会发现,使用Intent并不像其概念描述得那样难。最终创建Intent的代码如下所示。
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));
创建好Intent之后,你就可以通过它告诉Android希望启动新的Activity了。
startActivity(i);
Activity启动后显示界面如下:
5.总结说明
这篇文章是我刚开始学习Android时看到的,当时理解的不是很深入,现在再回头看这篇文章总结的很详细,在这里与大家分享
发表评论
-
Android的SoundPool类使用与利弊
2011-01-12 11:08 1104在开发Android软件中我们可能经常需播放多媒体声音文件,一 ... -
Android本地应用程序应用方式介绍
2011-01-11 17:57 695在我们曾经介绍的一片关于Android系统架构基本模式解析的文 ... -
android 轻松实现语音识别
2011-01-07 15:45 1365苹果的iphone 有语音识别用的是Google 的技术,做为 ... -
呼叫转移设置
2011-01-07 15:25 1189知电信网络提供商,你将呼叫转移了,转移到什么号码了。 所以这个 ... -
Android ExpandableListActivity 学习笔记(转载)
2011-01-07 10:56 1230An activity that displays an ex ... -
Wifi定位的原理是什么?
2011-01-07 10:33 1613与手机基站定位方式类 ... -
[Android 数据库] Android数据库总结
2011-01-07 10:05 1141任何的软件开发都离不 ... -
Intent 的工作机制
2011-01-07 10:00 553Android 中各个组件主要 ... -
Android权限说明
2011-01-07 09:59 696Android权限分的很细,但 ... -
详解Android组件的使用(转
2011-01-07 09:58 546Android开发平台是开放的 ... -
AsyncTask解决Android UI堵塞问题
2011-01-07 09:58 944平时我们在开发Andr ... -
消息机制细谈(message handler looper MessageQueue)
2011-01-07 09:54 759在handler的简单用法中, ...
相关推荐
主要为大家详细介绍了Android中的Intent Filter匹配规则,感兴趣的小伙伴们可以参考一下
本文在描述组件在manifest中注册的Intent Filter过滤器时,统一用intent-filter表示。 一、概述 我们知道,Intent是分两种的:显式Intent和隐式Intent。如果一个Intent明确指定了要启动的组件的完整类名,那么这个...
本文实例讲述了Android解析Intent Filter的方法。分享给大家供大家参考。具体分析如下: 匿名性质的运行时绑定使得理解Android如何...Intent解析的目的是通过下面的过程找到可能匹配得最好的Intent Filter: 1. An
学习基础随手笔记Intent对象对action的匹配规则: 1>Intent对象中只能含有一个action 2>intent-filter中可以声明多个action <intent-filter> </intent-filter> 3>如果intent中的action是Intent-filter中 声明...
如果对Intent Filter不是特别了解,可以参见《详解Android中Intent对象与Intent Filter过滤匹配过程》。 本文着重讲一下Android中一些常见的Intent的习惯用法,比如如何通过Intent发送短信、发送邮件、启动摄像机...
一个非常简单的Android intent 匹配过滤的例子,给初学者参考
如果要成功匹配该intent-filter我们需要完全匹配该intent-filter中的 action、category、data。 示例: <intent> <action android:name=com.blueberry.action></action> <action android:name
101要从黄铜调回黑铁了,太难了呀,都怪匹配给到的队友天天拉胯。。。(上单头铁盖伦打wn)。。Android的匹配机制也是给萌新当头一棒。但萌新突出一个莽字,不知道的东西就冲塔搞他。。首先我们萌新知道的启动...
文章目录第八讲Intent和BroadcastReceiverIntentIntent对象构成Component nameActionDataCategoryExtrasFlagsURI和intent-filter匹配:一个打电话和发短信示例1. 布局文件2. java文件3. 添加权限BroadcastReceiver...
文档通过代码实践总结了Activity的生命周期方法,Launcher Mode启动方式,Intent Filter的匹配规则。同时文档中用截图记录关键的Log信息。文件中也包含了代码可以方便的学习和理解Activity。
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...
1.3 Intent Filter的匹配规则 / 28 第2章 IPC机制 / 35 2.1 Android IPC简介 / 35 2.2 Android中的多进程模式 / 36 2.2.1 开启多进程模式 / 36 2.2.2 多进程模式的运行机制 / 39 2.3 IPC基础概念介绍 /...
16 1.2.2 Activity的Flags / 27 1.3 Intent Filter的匹配规则 / 28 第2章 IPC机制 / 35 2.1 Android IPC简介 / 35 2.2 Android中的多进程模式 / 36 2.2.1 开启多进程模式 / 36 2.2.2 多进程模式的运行机制 / ...