应用程序的组件为了告诉Android本身能响应、处理哪些隐式Intent请求,能够声明一个甚至多个Intent Filter。每一个Intent Filter描述该组件所能响应Intent请求的能力——组件但愿接收什么类型的请求行为,什么类型的请求数据。好比以前请求网页浏览器这个例子中,网 页浏览器程序的Intent Filter就应该声明它所但愿接收的Intent Action是WEB_SEARCH_ACTION,以及与之相关的请求数据是网页地址URI格式。如何为组件声明本身的Intent Filter? 常见的方法是在AndroidManifest.xml文件中用属性<Intent-Filter>描述组件的Intent Filter。java
前面咱们提到,隐式Intent(Explicit Intents)和Intent Filter(Implicit Intents)进行比较时的三要素是Intent的动做、数据以及类别。实际上,一个隐式Intent请求要可以传递给目标组件,必要经过这三个方面的检查。若是任何一方面不匹配,Android都不会将该隐式Intent传递给目标组件。接下来咱们讲解这三方面检查的具体规则。android
<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>的行为测试。ide
<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请求。测试
数据在<intent-filter>中的描述以下:code
<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,它们也须要相匹配才会经过测试。xml
讲解完Intent基本概念以后,接下来咱们就使用Intent激活Android自带的电话拨号程序,经过这个实例你会发现,使用Intent并不像其概念描述得那样难。最终建立Intent的代码以下所示。ci
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));
建立好Intent以后,你就能够经过它告诉Android但愿启动新的Activity了。it
startActivity(i);
Activity启动后显示界面以下:
这篇文章是我刚开始学习Android时看到的,当时理解的不是很深刻,如今再回头看这篇文章总结的很详细,在这里与你们分享。