原文:[置顶] Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)

接着上一篇的内容,下面我们再来看看Intent的Data与Type属性。

一、Data属性与Type属性

Data属性通常用于向Action属性提供操作的数据,Data属性接受一个Uri对象

格式:scheme://host:port/path

例如:

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

tel:123

其中prot部分被省略了,contacts/1是path部分。

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

二、Data属性与Type属性之间的关系

Type属性和Data同时出现会出现覆盖现象,如果要同时设置要使用setDataAndType()方法,举例如下:

	//Type属性被Data覆盖
public void overrideType(View source){
Intent intent = new Intent();
intent.setType("abc/xyz");
intent.setData(Uri.parse("lee://www.baidu.com:80/test"));
}
//Data属性被Type覆盖
public void overrideData(View source){
Intent intent = new Intent();
intent.setData(Uri.parse("lee://www.baidu.com:80/test"));
intent.setType("abc/xyz");
}
//同时设置Data和Type
public void dataAndType(View source){
Intent intent = new Intent();
intent.setDataAndType(
Uri.parse("lee://www.baidu.com:80/test"), "abc/xyz");
}

在manifest配置文件中配置Activity的方法如下

            <intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
<data
android:mimeType="" <!-- Type属性 -->
android:scheme="" <!-- scheme部分 -->
android:host="" <!-- host部分 -->
android:port="" <!-- port部分 -->
android:path="" <!-- path部分 -->
android:pathPrefix="" <!-- path前缀 -->
android:pathPattern=""/> <!-- path模版 -->
</intent-filter>

三、Data属性与Type属性的匹配方式

Type属性也是用户指定组件匹配的组件,Type是需要严格匹配的。

Data属性则不需要严格匹配,过程如下:

如果指定了android:scheme属性,那么只要Intent的Data属性的scheme部分与android:scheme属性相同就可以启动该组件。

如果制定了android:scheme属性和android:host需要两个属性与Intent的Data部分的该部分相同则启动该组件。

如果定制了android:scheme、android:host、android:port属性则需要与该部分相同才能启动该组件。

...................

四、小练习

Intent intent = new Intent();
String data = "http://blog.csdn.net/dawanganban";
Uri uri = Uri.parse(data);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);

Intent intent = new Intent();
String data = "content://com.android.contacts/contacts/1";
Uri uri = Uri.parse(data);
intent.setData(uri);
startActivity(intent);

Intent intent = new Intent();
String data = "tel:10000";
Uri uri = Uri.parse(data);
intent.setData(uri);
startActivity(intent);

五、Intent的其他属性

Extra属性:用于在多个Activity之间传递数据,该属性其实是一个Bundle对象,后面在Activity间传值的文章中将会介绍。

Flag属性:一些额外的控制旗标,在后面将会详细介绍。

Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)的更多相关文章

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

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

  2. Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值

    原文:[置顶] Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值 前面我们了解了如何启动一个Activity,一个Activity在启动另外一个Activity的时候 ...

  3. Android菜鸟的成长笔记(7)——什么是Activity

    原文:[置顶] Android菜鸟的成长笔记(7)——什么是Activity 前面我们做了一个小例子,在分析代码的时候我们提到了Activity,那么什么是Activity呢? Activity是An ...

  4. Android菜鸟的成长笔记(6)——剖析源码学自定义主题Theme

    原文:Android菜鸟的成长笔记(6)--剖析源码学自定义主题Theme 还记得在Android菜鸟的成长笔记(3)中我们曾经遇到了一个问题吗?"这个界面和真真的QQ界面还有点不同的就是上 ...

  5. Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service

    原文:Android菜鸟的成长笔记(17)-- 再看Android中的Unbounded Service 前面已经写过关于startService(Unbounded Service)的一篇文章:&l ...

  6. Android菜鸟的成长笔记(3)——给QQ登录界面说So Easy

    原文:Android菜鸟的成长笔记(3)--给QQ登录界面说So Easy 上一篇:Android菜鸟的成长笔记(2)--第一个Android应用 我们前面已经做了第一个Android应用程序,虽然有 ...

  7. Android菜鸟的成长笔记(2)——第一个Android应用

    原文:Android菜鸟的成长笔记(2)--第一个Android应用 上一篇:Android菜鸟的成长笔记(1)--Anddroid环境搭建从入门到精通 在上一篇Android菜鸟的成长笔记(1)中我 ...

  8. Android菜鸟的成长笔记(1)——Android开发环境搭建从入门到精通

    原文:Android菜鸟的成长笔记(1)--Android开发环境搭建从入门到精通 今天在博客中看到好多Android的初学者对Android的开发环境的搭建不熟悉而导致不能进行学习,所以我决定自己写 ...

  9. Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)

    原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...

随机推荐

  1. hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...

  2. 关于apche无缘无故个启动不了,解决方法

    1. 对于用户不小心把apache下的conf文件不小心给修改了,可那会导致,启动不了apache, 解决办法可以重新下载一个, 64为  32位  下载地址 http://www.veryhuo.c ...

  3. java socket线程通信

    关于socket线程通信的一些知识整理 一般我们需要要让两台机子进行通信,需要创建一个Server 类,一个Client类,还需要创建一个线程类 server public class Server ...

  4. WinForm - 格式化DataGridView单元格数据

    效果: 代码: /// <summary> /// 格式化数据 /// </summary> private void dataGridView1_CellFormatting ...

  5. scanf()常犯错误

    ------------------------------------------------------------------------ <> 本意:接收字符串. 写成代码:voi ...

  6. day8 - isinstance--issubclass-异常处理-自定义异常

    一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 __author__ = 'Administrator' class Foo(object): pass obj ...

  7. 知识点1-2:ASP.NET MVC背景

    1.发展阶段 CGI(公共网关接口)-->ASP(Active Server Pages,活动服务器页面)-->.NET 2. .NET平台 2002年初,微软发布了第一版.NET框架,这 ...

  8. Multiple bindings were found on the class path(转)

    Multiple bindings were found on the class path SLF4J API is designed to bind with one and only one u ...

  9. anroid里面的post请求

    一.需要用到的场景 在jQuery中使用$.post()就可以方便的发起一个post请求,在android程序中有时也要从服务器获取一些数据,就也必须得使用post请求了. 二.需要用到的主要类 在a ...

  10. .Net 社区虚拟大会”(dotnetConf)

    .Net 社区虚拟大会”(dotnetConf) “.Net 社区虚拟大会”(dotnetConf) 2016 今天凌晨在Channel9 上召开,在Scott Hunter的30分钟的 Keynot ...