关于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 ...
随机推荐
- Android DownloadThread.run()学习
android系统的下载代码写的很好,考虑的比较全面,值得我们学习.DownloadThread是其中执行下载的部分,下面从run进行研究. run(){ 一上来会设置一下下载线程的优先级:THREA ...
- Android 国际化图片资源文件
国际化 与字符串国际相似,在 res 下新建 drawable-zh 文件夹,存放中文环境下的图片 新建 drawable-en 作为英语环境下的图片 在 eclipse ...
- USACO4.12Beef McNuggets(背包+数论)
昨天晚上写的一题 结果USACO一直挂中 今天交了下 有一点点的数论知识 背包很好想 就是不好确定上界 官方题解: 这是一个背包问题.一般使用动态规划求解. 一种具体的实现是:用一个线性表储存所有的 ...
- poj3264Balanced Lineup(RMQ)
http://poj.org/problem?id=3264 RMQ讲解 http://dongxicheng.org/structure/lca-rmq/ j = log2K dp[i][j] = ...
- 根据价格范围筛选汽车(路由以及JS与Jquery)
通过输入价格范围,来筛选汽车,主要方法是通过点击“查询”按钮,触发chaxun()方法,利用Jquery和JS获取输入的值,然后为相应的div加载相应的动作,通过更改路由的路径,以此来实现筛选车辆,然 ...
- 【转】android JNI编程 一些技巧(整理)
原文网址:http://blog.csdn.net/linweig/article/details/5203716 本篇将介绍在JNI编程中如何传递参数和返回值. 首先要强调的是,native方法不但 ...
- NEsper使用的事件类型 z
NEsper使用的事件类型来描述事件的类型信息.你的应用在启动时可能预先配置定义事件类型,或者在运行时通过API或EPL语法动态的增加事件类型. EPL中的create schema 的语法允许在运行 ...
- ORM Entities vs. Domain Entities under Entity Framework 6.0
I stumbled upon the following two articles First and Second in which the author states in summary th ...
- [洛谷1580]yyy loves Easter_Egg I
题目背景 Soha的出题效率着实让人大吃一惊.OI,数学,化学的题目都出好了,物理的题还没有一道.于是,Huntfire,absi2011,lanlan对soha进行轮番炸,准备炸到soha出来,不料 ...
- 字符串逆转(递归和非递归java)
package 乒乒乓乓; public class 递归逆转字符串 { //非递归逆转 public static String reverse(String s) { ...