Understand---> Intent Filter

来源:互联网 发布:java bytebuffer put 编辑:程序博客网 时间:2024/06/06 03:01

应用程序的组件为了告诉Android自己能响应、处理哪些隐式Intent请求,可以声明一个甚至多个Intent Filter。每个Intent Filter描述该组件所能响应Intent请求的能力——组件希望接收什么类型的请求行为,什么类型的请求数据。

1.Action Test

<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.Category Test

<intent-filter>元素可以包含<category>子元素,比如:

<intent-filter> <category android:name=”android.Intent.Category.DEFAULT”/> <category android:name=”android.Intent.Category.BROWSABLE”/> </intent-filter> 

只有当Intent请求中所有的Category与组件中某一个Intent Filter的<category>完全匹配时,才会让该 Intent请求通过测试,Intent Filter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没有设置类别的Intent请求

3.Data Test

数据在<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被分成三部分来进行匹配:schemeauthoritypath。其中,用setData()设定的Intent请求的URI数据类型和scheme必须与Intent Filter中所指定的一致。若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。

4.Sample

说完Intent基本概念之后,接下来我们就使用Intent激活Android自带的电话拨号程序,通过这个实例你会发现,使用Intent并不像其概念描述的那样难。最终创建Intent的代码如下所示:

Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://10086″)); //启动新的ActivitystartActivity(i);

新的Activity启动后显示界面如下:


5.Summary

(1).web browser

Uri myBlogUri = Uri.parse("http://blog.csdn.net/jackhenry"); returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri); 

(2).map

Uri mapUri = Uri.parse("geo:38.899533,-77.036476"); returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

(3).dial 

Uri telUri = Uri.parse("tel:10086"); returnIt = new Intent(Intent.ACTION_DIAL, telUri);

(4).call

Uri callUri = Uri.parse("tel:10086"); returnIt = new Intent(Intent.ACTION_CALL, callUri);

(5).uninstall

Uri uninstallUri = Uri.fromParts("package", "xxx", null); returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

(6).install

Uri installUri = Uri.fromParts("package", "xxx", null); returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); 

(7).media player

Uri playUri = Uri.parse("file:///sdcard/download/beautiful.mp3"); returnIt = new Intent(Intent.ACTION_VIEW, playUri); 

(8).send a message to someone specified by the data

Uri emailUri = Uri.parse("mailto:jackhenryzh@yahoo.com.cn");returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);

(9).send mail

returnIt = new Intent(Intent.ACTION_SEND); String[] tos = { "jackhenryzh@yahoo.com.cn};String[] ccs = { "jackhenryzh@yahoo.com.cn};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).send SMS

Uri smsUri = Uri.parse("tel:10086"); returnIt = new Intent(Intent.ACTION_VIEW, smsUri); returnIt.putExtra("sms_body", "jackhenry"); returnIt.setType("vnd.android-dir/mms-sms");

(11).send mail directly

Uri smsToUri = Uri.parse("smsto://10086"); returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri); returnIt.putExtra("sms_body", "jackhenry");

 

(12).send MMS

Uri mmsUri = Uri.parse("content://media/external/images/emniem"); returnIt = new Intent(Intent.ACTION_SEND); returnIt.putExtra("sms_body", "jackhenry"); returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri); returnIt.setType("image/png");

用获取到的Intent直接调用startActivity(returnIt)ok了。

source address:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-3466.html