Data属性通常用于向Action属性提供操作的数据,Data属性接受一个Uri对象,一个Uri对象通常通过如下形式的字符串来表示:

content://com.android.contacts/contacts/1

tel:123

Uri字符串总满足如下格式:

scheme://host:port/path

例如上面给出content://com.android.contacts/contacts/1,其中content是scheme部分,com.android.contacts是host部分,port部分被省略了,/contacts/1 是path部分。

Type属性用于指定该Data所指定Uri对应的MIME类型,这种MIME类型可以是任何自定义的MIME类型,只要符合abc/xyz格式的字符串即可。

Data属性与Type属性的关系比较微妙,这两个属性会相互覆盖,例如:

  • 如果为Intent先设置Data属性,后设置Type属性,那么Type属性将会覆盖Data属性。
  • 如果为Intent先设置Type属性,后设置Data属性,那么Data属性将会覆盖Type属性。
  • 如果希望Intent既有Data属性,也有Type属性,应该调用Intent的setDataAndType()方法。

下面的示例演示了Intent的Data与Type属性互相覆盖的情形,该示例的界面布局文件很简单,只定义了三个按钮,并为三个按钮绑定了事件监听器。

下面是该实例的Activity代码。

package com.example.studyintent;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast; public class DataTypeOverride extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_type_override);
} public void overrideType(View source)
{
Intent intent=new Intent();
//先为Intent设置Type属性
intent.setType("abc/xyz");
//再为Intent设置Data属性,覆盖Type属性
intent.setData(Uri.parse("lee://www.fkjava.org:8888/test"));
Toast.makeText(this, intent.toString(),Toast.LENGTH_LONG).show();
} public void overrideData(View source)
{
Intent intent=new Intent();
//先为Intent设置Data属性
intent.setData(Uri.parse("lee://www.fkjava.org:8888/mypath"));
//再为Intent设置Type属性,覆盖Data属性
intent.setType("abc/xyz");
Toast.makeText(this, intent.toString(),Toast.LENGTH_LONG).show();
} public void dataAndType(View source)
{
Intent intent=new Intent();
//同时设置Intent的Data、Type属性
intent.setDataAndType(Uri.parse("lee://www.fkjava.org:8888/mypath"), "abc/xyz");
Toast.makeText(this, intent.toString(), Toast.LENGTH_LONG).show();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.data_type_override, menu);
return true;
} }

上面的三个事件监听方法分别为Intent设置了Data、Type属性,第一个事件监听方法先设置Type属性,再设置Data属性,这将导致Data属性覆盖Type属性,单击按钮激发该事件监听方法,将可以看到如图5.6所示的Toast输出。

从图5.6可以看出,此时的Intent只有Data属性,Type属性被覆盖了。

上面的示例中第二个事件监听方法先设置了Data属性、再设置了Type属性,这将导致Type属性覆盖Data属性,单击按钮激发该事件监听方法,将可以看到如图5.7所示输出。

上面的示例中第三个事件监听方法同时设置了Data、Type属性、这样该Intent才会同时具有Data、Type属性。

在AndroidManifest.xml文件中为组件声明Data、Type属性都通过<data.../>元素,<data.../>元素的格式如下:

<data android:mimeType=""
android:scheme=""
android:host=""
android:port=""
android:path=""
android:pathPrefix=""
android:pathPattern=""/>

上面<data.../>元素支持如下属性。

  • mimeType:用于声明该组件所能匹配的Intent的Type属性。
  • scheme:用于声明该组件所能匹配的Intent的Data属性的scheme部分。
  • host:用于声明该组件所能匹配的Intent的Data属性的host部分。
  • port:用于声明该组件所能匹配的Intent的Data属性的port部分。
  • path:用于声明该组件所能匹配的Intentde Data属性的path部分。
  • pathPrefix:用于声明该组件所能匹配的Intent的Data属性的path前缀。
  • pathPattern:用于声明该组件所能匹配的Intent的的Data属性的path字符串模板。

Intent的Type属性也用于指定该Intent的要求,必须对应组件中<intent-filter.../>元素中<data.../>子元素的mineType属性与此相同,才能启动该组件。

Data属性的“匹配”过程有些差别,它会先检查<intent-filter.../>里的<data.../>子元素然后:

  • 如果目标组件的Data子元素只指定了android:scheme属性,那么只要Intent的Data属性的scheme部分与android:scheme属性值相同,即可启动该组件。
  • 如果目标组件的<data.../>子元素只指定了android:scheme、android:host属性,那么只要Intent的Data属性的scheme、host部分与android:scheme、android:host属性值相同,即可启动该组件。
  • 如果目标组件的<data.../>子元素指定了android:scheme、android:host、android:port属性,那么要求Intent的Data属性的scheme、host、port部分与android:scheme、android:host、android:host属性值相同,即可启动该组件。
  • 如果目标组件的<data.../>子元素只指定了android:scheme、android:host、android:path属性,那么只要求Intent的Data属性的scheme、host、port部分与android:scheme、android:host、android:path属性值相同,即可启动该组件。
  • 如果目标组件的<data.../>子元素指定了android:scheme、android:host、android:port、android:path属性,那么就要求Intent的Data属性scheme、host、port、path部分依次与android:scheme、android:host、android:port、android:path属性值相同,才可启动该组价。

下面的示例测试了Intent的Data属性与<data.../>元素配置的关系,该示例依次配置了如下5个Activity。

 <activity
android:name="com.example.studyintent.SchemeActivity"
android:icon="@drawable/ic_scheme"
android:label="指定scheme的Activity" >
<intent-filter>
<action android:name="xx" /> <category android:name="android.intent.category.DEFAULT" />
<!-- 只要Intent的Data属性的scheme是lee,即可启动该Activity -->
<data android:scheme="lee" />
</intent-filter>
</activity>
<activity
android:name="com.example.studyintent.SchemeHostPortActivity"
android:icon="@drawable/ic_host"
android:label="指定scheme、host、port的Activity" >
<intent-filter>
<action android:name="xx" /> <category android:name="android.intent.category.DEFAULT" />
<!-- 只要Intent的Data属性和scheme是lee,且host是www.fkjava.orgport是8888即可启动该Activity -->
<data
android:host="www.fkjava.org"
android:port="8888"
android:scheme="lee" />

</intent-filter>
</activity>
<activity
android:name="com.example.studyintent.SchemeHostPathActivity"
android:icon="@drawable/ic_sp"
android:label="指定scheme、host、path的Activity" >
<intent-filter>
<action android:name="xx" /> <category android:name="android.intent.category.DEFAULT" />
<!--
只要Intent的Data属性的scheme是lee,且host是www.fkjava.org path是/maypath,
即可启动该Activity -->
<data
android:host="www.fkjava.org"
android:path="/mypath"
android:scheme="lee" />

</intent-filter>
</activity>
<activity
android:name="com.example.studyintent.SchemeHostPortPathActivity"
android:icon="@drawable/ic_path"
android:label="指定scheme、host、port、path的Activity" >
<intent-filter>
<action android:name="xx" /> <category android:name="android.intent.category.DEFAULT" />
<!--
需要Intent的Data属性的scheme是lee,且host是www.fkjava.org port是8888,
且path是/mypath,才可启动该Activity -->
<data
android:host="www.fkjava.org"
android:path="/mypath"
android:port="8888"
android:scheme="lee" />

</intent-filter>
</activity>
<activity
android:name="com.example.studyintent.SchemeHostPortPathTypeActivity"
android:icon="@drawable/ic_type"
android:label="指定scheme、host、port、path、type的Activity" >
<intent-filter>
<action android:name="xx" /> <category android:name="android.intent.category.DEFAULT" />
<!--
需要Intent的Data属性的scheme是lee,且host是www.fkjava.org port是8888,
且path是/mypath 且type是abc/xyz,才可启动该Activity -->
<data
android:host="www.fkjava.org"
android:mimeType="abc/xyz"
android:path="/mypath"
android:port="8888"
android:scheme="lee" />

</intent-filter>
</activity>

上面的配置文件中配置了5个Activity,这个5个Activity的实现类都非常简单,它们都仅在界面上显示一个TextView,并不显示其他内容。关于这5个Activity的<data.../>子元素配置的说明如下:

  • 第一个Activity:只要Intent的Data属性的scheme是lee,即可启动该Activity。
  • 第二个Activity:只要Intent的Data属性的scheme是lee,且host是www.fkjava.org、port是8888,即可启动该Activity。
  • 第三个Activiyt:只要Intent的Data属性的scheme是lee,且host是www.fkjava.org、path是/mypath,即可启动该Activiyt。
  • 第四个Activiyt:需要Intent的Data属性的scheme是lee,且host是www.fkjava.org、port是8888、且path是/mypath,才可启动该Activity。
  • 第五个Activity:需要Intent的Data属性的scheme是lee,且host是www.fkjava.org、port是8888、且path是/mypaht,且type是abc/xyz,才可启动该Activity。

下面是启动第一个Activity的方法:

  public void scheme(View source)
{
Intent intent=new Intent();
//只设置Intent的Data属性
intent.setData(Uri.parse("lee://www.crazyit.org:1234:test"));
startActivity(intent);
}

由于上面的Data属性,只有scheme为lee,也就是只有1个Activity符合条件,因此通过该方法启动Activity时,将可以看到如图5.8所示的Activity。

下面是第二个启动Activity的方法:

public void schemeHostPort(View source)
{
Intent intent=new Intent();
//只设置Intent的Data属性
intent.setData(Uri.parse("lee://www.fkjava.org:8888/test"));
startActivity(intent);
}

由于上面的Intent的Data属性,只有scheme为lee,也就是有第一个Activity符合条件;且该Intent的Data属性的host为www.fkjava.org、port为8888,因此第二个Activity也符合条件。通过该方法启动Activity时将可看到启动5.9所示的选择Activity的界面。

下面是第3个启动Activity的方法:

public void schemeHostPath(View source)
{
Intent intent=new Intent();
//只设置Intent的Data属性
intent.setData(Uri.parse("lee://www.fkjava.org:1234/mypath"));
startActivity(intent);
}

由于上面Intent的Data属性,只有scheme为lee,也就是有第一个Activity符合条件;且该Intent的Data属性的host为www.fkjava.org、path为/mypath,因此第三个Activity也符合条件。通过该方法启动Activity时,即可看到启动如图5.10所示的选择Activity的界面。

下面是第4个启动Activity的方法。

public void schemeHostPortPath(View source)
{
Intent intent=new Intent();
//只设置Intent的Data属性
intent.setData(Uri.parse("lee://www.fkjava.org:8888/mypath"));
startActivity(intent);
}

由于上面Intent的Data属性,只有scheme为lee,也就只有第一个Activity符合条件;且该Intent的Data属性为的host为www.fkjava.org、port为8888,因此第二个Activity也符合条件;且该Intent的Data属性的host为www.fkjava.org、path为/mypath,因此第三个Activity也符合条件;且该Intent的Data属性的host为www.fkjava.org、port为8888、path为/mypath,因此第四个Activity也符合条件。通过该方法启动Activity时,将可看到启动如图5.11所示的选择Activity的界面。

下面是第5个启动Activity的方法:

public void schemeHostPortPathType(View source)
{
Intent intent=new Intent();
//同时设置Intent的Data、Type属性
intent.setDataAndType(Uri.parse("lee://www.fkjava.org:8888/mypath"), "abc/xyz");
startActivity(intent); }

上面的Intent不仅指定了Data属性,也指定了Type属性,此时符合条件的只有第五个Activity,通过该方法启动Activity时,将可看到启动如图5.12所示的Activity。

Intent的属性及Intent-filter配置——Data、Type属性与intent-filter配置的更多相关文章

  1. 【配置属性】—Entity Framework 对应表字段的类型的设定配置方法

    摘自:http://www.cnblogs.com/nianming/archive/2012/11/07/2757997.html Entity Framework Code First的默认行为是 ...

  2. 为什么要在<button>元素中添加type属性

    在HTML中<button> 标签定义一个按钮. <button type="button">Click Me!</button> 在 butt ...

  3. Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性

    在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...

  4. Spring中<bean>标签之使用p标签配置bean的属性

    在spring的bean配置文件中我们常可以见到下面的例子: <bean id="user" class="com.sys.User" p:name-re ...

  5. 【转】logback logback.xml常用配置详解(三) <filter>

    原创文章,转载请指明出处:http://aub.iteye.com/blog/1110008, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...

  6. logback logback.xml常用配置详解(三) <filter>

    <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一.返回DENY,日志将立即被抛弃不再经过其他过滤器:返回NEUTRAL,有序列表 ...

  7. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. (转)Spring boot——logback.xml 配置详解(四)<filter>

    文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 filter的使用 <filter>: Logback的过滤器基于三值逻辑( ...

  9. 笔记:MyBatis XML配置-Settings 完整属性表

    设置参数 描述 有效值 默认值 cacheEnabled 该配置影响的所有映射器中配置的缓存的全局开关. true | false true lazyLoadingEnabled 延迟加载的全局开关. ...

随机推荐

  1. Mybatis的<where><foreach><set>等标签详解

    sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空 时,是查出全部的信息.这是我们可以使用动态sql,增加一个判断, ...

  2. SqlServer批量导入

    SQL Server的Bulk Insert语句可以将本地或远程的数据文件批量导入到数据库中,速度非常的快.远程文件必须共享才行,文件路径须使用通用约定(UNC)名称,即"\\服务器名或IP ...

  3. 基于Verilog HDL的ADC0809CCN数据采样

    本实验是用ADC0809CCN进行数据采样,并用7段数码管进行显示. ADC0809由一个8路模拟开关.一个地址锁存与译码器.一个A/D转换器和一个三态输出锁存器组成.多路开关可选通8个模拟通道,允许 ...

  4. gcd timer

    //0.创建队列 dispatch_queue_t queue = dispatch_get_global_queue(, ); NSLog(@"%s",__func__); // ...

  5. PDF 补丁丁 0.5 正式版发布

    经过了两年的测试,新版本的 PDF 补丁丁已经比较稳定了.在农历新年前发布这个 0.5 版,作为正式稳定版吧. 新的 PDF 补丁丁比旧的 0.3 版增加了许多功能: PDF 可视化编辑文档书签,可从 ...

  6. JavaScript 逗号表达式

    逗号表达式的一般形式是:表达式1,表达式2,表达式3……表达式n  逗号表达式的求解过程是:先计算表达式1的值,再计算表达式2的值,……一直计算到表达式n的值.最后整个逗号表达式的值是表达式n的值.  ...

  7. HDU 2489 Minimal Ratio Tree 最小生成树+DFS

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. PHP 删除非法UTF-8字符

    //reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ? $some_ ...

  9. C# 开发系列(二)

    1. 参考文档:http://www.yiibai.com/csharp/csharp_environment_setup.html 2. C# ,ASP.NET HTTP Authorization ...

  10. 将Web项目访问的URL项目名设置为"/"

    工具:Eclipse 步骤: 1.鼠标右键项目名--->properties--->Web Project Setting--->Context root. 将Context roo ...