关于Intent的七大属性
原谅我愚昧,Intent七大属性这个概念我也是昨天才接触到,看了一下,都是一些常用的东西,就是没有总结过,那么今天就来简单总结一下。
Intent七大属性是指Intent的ComponentName、Action、Category、Data、Type、Extra以及Flag,七个属性,总体上可以分为3类:
第一类:启动,有ComponentName(显式),Action(隐式),Category(隐式)。
第二类:传值,有Data(隐式),Type(隐式),Extra(隐式、显式)。
第三类:启动模式,有Flag。
下面我们逐一来说。
1.ComponentName
Component本身有组件的意思,我们通过设置Component可以启动其他的Activity或者其他应用中的Activity,来看一个简单的实例:
启动同一个App中另外一个Activity:
intent = new Intent();
intent.setComponent(new ComponentName(this, SecondActivity.class));
startActivity(intent);
这中启动方式等同于以下两种启动方式:
intent = new Intent(this,SecondActivity.class);
startActivity(intent);
intent = new Intent();
intent.setClass(this, SecondActivity.class);
startActivity(intent);
当然,通过设置ComponentName属性我们也可以启动其他App中的Activity,关于这一块的内容大家可以参考关于ComponentName的使用。下面我们看看隐式启动。
2.Action和Category
因为在实际开发中,Action大多时候都是和Category一起使用的,所以这里我们将这两个放在一起来讲解。Intent中的Action我们在使用广播的时候用的比较多,在Activity中,我们可以通过设置Action来隐式的启动一个Activity,比如我们有一个ThirdActivity,我们在清单文件中做如下配置:
<activity
android:name=".ThirdActivity"
android:label="@string/title_activity_third" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" /> <action android:name="com.qf.ThirdActivity" />
</intent-filter>
</activity>
当我们在清单文件中做了这样的配置之后,我们的ThirdActivity会就会响应这个动作,怎么那么怎么响应呢?看下面:
intent = new Intent();
intent.setAction("com.qf.ThirdActivity");
startActivity(intent);
当然,我们也可以写的更简单一些,如下:
intent = new Intent("com.qf.ThirdActivity");
startActivity(intent);
通过这中方式我们也可以启动一个Activity,那么大家可能也注意到了,我们的清单文件中有一个category的节点,那么没有这个节点可以吗?不可以!!当我们使用这种隐式启动的方式来启动一个Activity的时候,必须要action和category都匹配上了,该Activity才会成功启动。如果我们没有定义category,那么可以暂时先使用系统默认的category,总之,category不能没有。这个时候我们可能会有疑问了,如果我有多个Activity都配置了相同的action,那么会启动哪个?看看下面这个熟悉的图片:
当我们有多个Activity配置了相同的action的时候,那么系统会弹出来一个选择框,让我们自己选择要启动那个Activity。
action我们只能添加一个,但是category却可以添加多个(至少有一个,没有就要设置为DEFAULT),如下:
<activity
android:name=".ThirdActivity"
android:label="@string/title_activity_third" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="mycategory" /> <action android:name="com.qf.ThirdActivity" />
</intent-filter>
</activity>
相应的我们的启动方式也可以修改,如下:
intent = new Intent("com.qf.ThirdActivity");
intent.addCategory("mycategory");
startActivity(intent);
3.Data
通过设置data,我们可以执行打电话,发短信,开发网页等等操作。究竟做哪种操作,要看我们的数据格式:
// 打开网页
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
// 打电话
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("tel:18565554482"));
startActivity(intent);
当我们的data是一个http协议的时候,系统会自动去查找可以打开http协议的Activity,这个时候如果手机安装了多个浏览器,那么系统会弹出多个浏览器供我们选择。这是我们通过设置Data来启动一个Activity,同时,我们也可以通过设置一个Data属性来将我们的Activity发布出去供别人调用,怎么发布呢?
<activity
android:name=".HttpActivity"
android:label="@string/title_activity_http" >
<intent-filter>
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data
android:scheme="http" />
</intent-filter>
</activity>
在data节点中我们设置我们这个Activity可以打开的协议,我们这里设置为http协议,那么以后要打开一个http请求的时候,系统都会让我们选择是否用这个Activity打开。当然,我们也可以自己定义一个协议(自己定义的协议,由于别人不知道,所以只能由我们自己的程序打开)。比如下面这样:
<activity
android:name=".HttpActivity"
android:label="@string/title_activity_http" >
<intent-filter>
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data
android:scheme="myhttp" />
</intent-filter>
</activity>
那么我们怎么打开自己的Activity呢?
intent = new Intent();
intent.setData(Uri.parse("myhttp://www.baidu.com"));
startActivity(intent);
这个例子没有什么实际意义,我只是举一个自定义协议的栗子。
其实,说到这里,大家应该明白了为什么我们说data是隐式传值,比如我们打开一个网页,http协议后面跟的就是网页地址,我们不用再单独指定要打开哪个网页。
4.Type
type的存在,主要是为了对data的类型做进一步的说明,但是一般情况下,只有data属性为null的时候,type属性才有效,如果data属性不为null,系统会自动根据data中的协议来分析data的数据类型,而不会去管type,我们先来看看下面一段源码:
/**
* Set the data this intent is operating on. This method automatically
* clears any type that was previously set by {@link #setType} or
* {@link #setTypeAndNormalize}.
*
* <p><em>Note: scheme matching in the Android framework is
* case-sensitive, unlike the formal RFC. As a result,
* you should always write your Uri with a lower case scheme,
* or use {@link Uri#normalizeScheme} or
* {@link #setDataAndNormalize}
* to ensure that the scheme is converted to lower case.</em>
*
* @param data The Uri of the data this intent is now targeting.
*
* @return Returns the same Intent object, for chaining multiple calls
* into a single statement.
*
* @see #getData
* @see #setDataAndNormalize
* @see android.net.Uri#normalizeScheme()
*/
public Intent setData(Uri data) {
mData = data;
mType = null;
return this;
} /**
* Set an explicit MIME data type.
*
* <p>This is used to create intents that only specify a type and not data,
* for example to indicate the type of data to return.
*
* <p>This method automatically clears any data that was
* previously set (for example by {@link #setData}).
*
* <p><em>Note: MIME type matching in the Android framework is
* case-sensitive, unlike formal RFC MIME types. As a result,
* you should always write your MIME types with lower case letters,
* or use {@link #normalizeMimeType} or {@link #setTypeAndNormalize}
* to ensure that it is converted to lower case.</em>
*
* @param type The MIME type of the data being handled by this intent.
*
* @return Returns the same Intent object, for chaining multiple calls
* into a single statement.
*
* @see #getType
* @see #setTypeAndNormalize
* @see #setDataAndType
* @see #normalizeMimeType
*/
public Intent setType(String type) {
mData = null;
mType = type;
return this;
}
当我们设置data的时候,系统会默认将type设置为null,当我们设置type的时候,系统会默认将data设置为null.也就是说,一般情况下,data和type我们只需要设置一个就行了,如果我们既想要设置data又想要设置type,那么可以使用
setDataAndType(Uri data, String type)
这个方法来完成。下面我们来看看通过给Intent设置type来打开一个音乐播放器。代码如下:
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri data = Uri.parse("file:///storage/emulated/0/xiami/audios/被动.mp3");
intent.setDataAndType(data, "audio/mp3");
startActivity(intent);
如果我们要打开的是视频文件,那么type就要设置为"video/*",其中*表示支持所有的视频文件。
5.Extra
Extra就比较好理解了,我们经常使用它来在Activity之间传递数据,Extra可以传递基本类型,String类型以及实现了Serializable或者Parcelable接口的类,具体用法不多说。
6.Flag
通过设置Flag,我们可以设定一个Activity的启动模式,这个和launchMode基本上是一样的,所以我也不再细说,关于launchMode的使用参见launchMode使用详解
关于Intent的七大属性的更多相关文章
- 06 Activity的启动模式 Intent的七大属性的总结
1.Task以及back stack >Task(任务) 为了完成一个功能 多个Activity的集合, 当你的应用程序启动时 系统会自动创建Task用于管理Activity ...
- Intent的七大属性
1.Action Action属性代表系统要执行的动作 系统提供如下常用的Action属性 *ACTION_MAIN:应用程序入口点 *ACTION_VIEW:显示指定数据 *ACTION_EDIT: ...
- 关于Intent的七大重要属性
在Android 中,Intent用来封装两个Activity之间的调用意图,实现两个Activity之间的跳转,并传递信息. Intent的七大重要属性:ComponentName Action ...
- Intent七大属性之总结
参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...
- Intent七大属性之总结 分类: H1_ANDROID 2013-11-10 09:41 1074人阅读 评论(0) 收藏
参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...
- Intent七大属性
一.Intent的作用是什么? 1.Intent 用于封装程序的”调用意图“.两个Activity之间,可以把需要交换的数据封装成Bundle对象,然后使用Intent携带Bundle对象,实现 ...
- Intent的七大组件——Android开发之路5
------Intent------ Android中三个核心组件——Activity.Services.BroadCastProvider都是通过Intent传递参数. startActivity( ...
- Intent 的Flag属性(Activity在栈位置的主宰者)
Intent 的Flag属性可用来控制Activity在栈中的排列位置,本文列举了常见的Flag. 例--(以无动画方式启动ActivityB): Intent intent=new Intent(A ...
- Intent七在属性之一:ComponentName
注:在<疯狂android讲义>中,此属性称为Component,官方文档中称为ComponentName. 1.The name of the component that should ...
随机推荐
- java1.8的几大新特性(一)
一.接口的默认方法与静态方法,也就是接口中可以有实现方法 public class Test { public static void main(String[] args) { Formula a= ...
- insmod: error inserting 'simp-blkdev.ko': -1 Invalid module format
(一) 今天写了个块设备驱动例子,在虚拟机上加载模块insmod simp_blkdev.ko的时候,出现以下错误. insmod: error inserting 'simple-blk.ko': ...
- 结构体dtype_t
/* SQL data type struct */ typedef struct dtype_struct dtype_t; struct dtype_struct{ unsigned mtype: ...
- bzoj2506
不知道该给这题贴什么标签了,但是这是一道好题1.注意这道题的询问是满足区间减法的,我们可以把一个询问拆成两个询问离线搞搞,这个思想在后面会见到2.分类的思想,这道题的求相同余数看似很烦,实际上我们发现 ...
- 删除一个目录和其各级子目录下的.svn文件
两种方法[1]用find命令和其action来实现[2]用rm直接实现$ cd /tmp/xxx$ rm -rf `find . -name .svn`就可以实现了. 删除SVN目录及从服务器端删除 ...
- 在C#中设置打印机纸张大小
using System.Drawing.Printing.PrintDocument using System.Drawing.Printing.PrinterSettings.PaperSizeC ...
- [codevs1554]最佳课题选择
题目描述 Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同.具体地说,对于某个课题i ...
- wuzhicms查找:当前页面使用的哪个文件
要查看这个地址的模版.http://www.wuzhicms.com/item-34-2-1.html 首先,我们的这个地址需要是动态的.而不是生成的静态地址. 打开文件: /www/configs/ ...
- bzoj 1975 [Sdoi2010]魔法猪学院(k短路)
题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元素与 ...
- MTRR内存类型范围寄存器
1.MTRR的概念 内存类型范围寄存器(MTRRs,翻译过来真别扭,后面都以MTRR直接来说了)提供了一种机制,这种机制其实就是确定在系统内存中物理一段内存的类型.这个类型其实是正对CPU来说的,见图 ...