intent intent-filter

应用程序的组件为了告诉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时看到的,当时理解的不是很深入,现在再回头看这篇文章总结的很详细,在这里与大家分享。 



1,掉web浏览器 

Uri myBlogUri = Uri.parse("http://kuikui.iteye.com"); 

returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri); 

2,地图 

Uri mapUri = Uri.parse("geo:38.899533,-77.036476"); 

returnIt = new Intent(Intent.ACTION_VIEW, mapUri); 

3,调拨打电话界面 

Uri telUri = Uri.parse("tel:100861"); 

returnIt = new Intent(Intent.ACTION_DIAL, telUri); 

4,直接拨打电话 

Uri callUri = Uri.parse("tel:100861"); 

returnIt = new Intent(Intent.ACTION_CALL, callUri); 

5,卸载 

Uri uninstallUri = Uri.fromParts("package", "xxx", null); 

returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri); 

6,安装 

Uri installUri = Uri.fromParts("package", "xxx", null); 

returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); 

7,播放 

Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3"); 

returnIt = new Intent(Intent.ACTION_VIEW, playUri); 

8,掉用发邮件 

Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com"); 

returnIt = new Intent(Intent.ACTION_SENDTO, emailUri); 

9,发邮件 

returnIt = new Intent(Intent.ACTION_SEND); 

String[] tos = { "shenrenkui@gmail.com" }; 

String[] ccs = { "shenrenkui@gmail.com" }; 

returnIt.putExtra(Intent.EXTRA_EMAIL, tos); 

returnIt.putExtra(Intent.EXTRA_CC, ccs); 

returnIt.putExtra(Intent.EXTRA_TEXT, "body"); 

returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject"); 

returnIt.setType("message/rfc882"); 

Intent.createChooser(returnIt, "Choose Email Client"); 

10,发短信 

Uri smsUri = Uri.parse("tel:100861"); 

returnIt = new Intent(Intent.ACTION_VIEW, smsUri); 

returnIt.putExtra("sms_body", "shenrenkui"); 

returnIt.setType("vnd.android-dir/mms-sms"); 

11,直接发邮件 

Uri smsToUri = Uri.parse("smsto://100861"); 

returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri); 

returnIt.putExtra("sms_body", "shenrenkui"); 

12,发彩信 

Uri mmsUri = Uri.parse("content://media/external/images/media/23"); 

returnIt = new Intent(Intent.ACTION_SEND); 

returnIt.putExtra("sms_body", "shenrenkui"); 

returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri); 

returnIt.setType("image/png"); 

用获取到的Intent直接调用startActivity(returnIt)就ok了。
posted @ 2013-08-07 09:34  起始页  阅读(420)  评论(0编辑  收藏  举报