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. < intent-filter >
2. < action android:name="com.example.project.SHOW_CURRENT" />
3. < action android:name="com.example.project.SHOW_RECENT" />
4. < action android:name="com.example.project.SHOW_PENDING" />
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. < intent-filter . . . >
2. < category android:name="android.Intent.Category.DEFAULT" />
3. < category android:name="android.Intent.Category.BROWSABLE" />
4. </ intent-filter >
只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请求通过测试
,IntentFilter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没
有设置类别的Intent请求。

3.数据测试

数据在<intent-filter>中的描述如下:
view source print ?
1. < intent-filter . . . >
2. < data android:type="video/mpeg" android:scheme="http" . . . />
3. < data android:type="audio/mpeg" android:scheme="http" . . . />
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 )。

参考:http://blog.csdn.net/wuwenxiang91322/article/details/7671593

二、代码实例

程序实例

点击后

com.fry.intent_Filter2.MainActivity

 package com.fry.intent_Filter2;

 import com.example.activity.R;

 import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity{
private Button btn_openActivty;//创建一个button对象
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父类操作
setContentView(R.layout.activity_main);//引入名为activity_main的界面
btn_openActivty=(Button) findViewById(R.id.btn_openActivity);//找id为btn_openActivity的button
btn_openActivty.setOnClickListener(new OnClickListener() {//设置button点击监听 @Override
public void onClick(View v) {//onclick事件
// TODO Auto-generated method stub
Intent intent=new Intent();//初始化intent
/**
* 找所有action符合为"com.fry.activity01"的页面
* 也找所有Category为android.intent.category.DEFAULT的页面
* String android.content.Intent.CATEGORY_DEFAULT =
* "android.intent.category.DEFAULT"
*/
intent.setAction("com.fry.activity01");
intent.addCategory(Intent.CATEGORY_DEFAULT);
//当只有data没有type的时候
//intent.setData(Uri.parse("http://www.baidu.com:8080/image"));
intent.setDataAndType(Uri.parse("http://www.baidu.com:8080/image"), "text/plain");
startActivity(intent);//打开activity
}
});
}
}

/intent_Filter2/AndroidManifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.fry.intent_Filter2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.fry.intent_Filter2.Activity01">
<intent-filter >
26 <action android:name="com.fry.activity01"></action>
27 <category android:name="android.intent.category.DEFAULT"/>
28 <data android:scheme="http" android:host="www.baidu.com" android:port="8080" android:path="/image" android:mimeType="text/plain"></data>

</intent-filter>
</activity>
</application> </manifest>

intent Filter的更多相关文章

  1. Android中的Intent Filter匹配规则介绍

    本文主要介绍了隐式Intent匹配目标组件的规则,若有叙述不清晰或是不准确的地方希望大家指出,谢谢大家: ) 1. Intent简介 Intent用于在一个组件(Component,如Activity ...

  2. Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)

    原文:[置顶] Android菜鸟的成长笔记(9)——Intent与Intent Filter(下) 接着上一篇的内容,下面我们再来看看Intent的Data与Type属性. 一.Data属性与Typ ...

  3. Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)

    原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...

  4. Android 4 学习(13):Local Broadcast Manager & Intent Filter

    参考<Professional Android 4 Development> Local Broadcast Manager 简介 Local Broadcast Manager由Andr ...

  5. android intent filter浏览器应用的设置,如何使用choose-box选择应用

    //使用chooserIntent private void startImplicitActivation() { Log.i(TAG, "Entered startImplicitAct ...

  6. Android Google官方文档(cn)解析之——Intents and Intent filter

    应用程序核心组件中的三个Activity,service,还有broadcast receiver都是通过一个叫做intent的消息激活的.Intent消息传送是在相同或不同的应用程序中的组件之间后运 ...

  7. Intent和Intent Filter Context

    http://www.android-doc.com/reference/android/content/Intent.html An intent is an abstract descriptio ...

  8. Android之旅-Intent与Intent Filter[上]

    Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指定组件,至于到底启动哪个组件,取决于Intent的各个属性. 一.显式的Intent 明确指定了要启动 ...

  9. 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent

    Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...

随机推荐

  1. 单舵轮(叉车)AGV里程计数据解算

    单舵轮(叉车)AGV里程计数据解算 2016-07 单舵轮AGV,一般包含一个驱动轮和两个从动轮,驱动轮是同时具备行走和转向两个功能的舵轮,因此,单舵轮AGV的运动学自由度为2个.舵轮线速度V1,舵轮 ...

  2. 安装pip环境以及pip常用命令使用

    1.去到Python的官网下载pip包,下载地址是:https://pypi.python.org/pypi/pip#downloads 2.下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录 ...

  3. MongoDB的分布式部署

    一.分片的概念 分片(sharding)是指根据片键,将数据进行拆分,使其落在不同的机器上的过程.如此一来,不需要功能,配置等强大的机器,也能储存大数据量,处理更高的负载. 二.分片的原理和思想 Mo ...

  4. matplotlib中的legend()—显示图例

    源自  matplotlib中的legend()——用于显示图例 -- 博客园 http://www.cnblogs.com/yinheyi/p/6792120.html legend()的一个用法: ...

  5. gdb core

    程序运行发生异常退出,比如segment错误,此时可以利用系统生成的core文件,配合GDB来定位问题. 问题程序: segment.c #include <stdio.h> #inclu ...

  6. 设置 Quick-Cocos2d-x 在 Windows 下的编译环境

    http://cn.cocos2d-x.org/tutorial/show?id=1304 设置 Quick-Cocos2d-x 在 Windows 下的编译环境 Liao Yulei2014-08- ...

  7. PKU 1379 Run Away(模拟退火算法)

    题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用 ...

  8. POJ - 2175 Evacuation Plan (最小费用流消圈)

    题意:有N栋楼,每栋楼有\(val_i\)个人要避难,现在有M个避难所,每个避难所的容量为\(cap_i\),每个人从楼i到避难所j的话费是两者的曼哈顿距离.现在给出解决方案,问这个解决方案是否是花费 ...

  9. spark-sql做ETL时遇到的两个问题

    项目中使用spark-sql来作ETL,遇到两个问题,记录一下. 问题1: spark-sql –master yarn –hiveconf load_date=`date –d ..`  -e 'i ...

  10. java文件遍历

    用java实现本地文件的遍历,顺便了解了下集合框架,注意java中还有Collections,是一个强大的工具,注意其与Collection的区别,在 for(File f: listFiles) 中 ...