关于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 ...
随机推荐
- 如何kill掉TaobaoProtect.exe
C:\Users\Administrator\AppData\Roaming\TaobaoProtect TaobaoProtect.exe https://technet.microsoft.com ...
- APP-PAY-06153 When Trying To Open Organization Definition Form (文档 ID 1323165.1)
In this Document Symptoms Cause Solution Applies to: Oracle Inventory Management - Version 11.5.10.2 ...
- c语言字符串库函数#include<string.h>
字符串函数<string.h> 在头文件<string.h>中定义了两组字符串函数.第一组函数的名字以str开头:第二组函数的名字以mem开头.只有函数memmove对重叠对象 ...
- spring--处理器拦截器详解——跟着开涛学SpringMVC
5.1.处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器) 类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理. ...
- lightoj 1013
思路:动态规划.设dp[i][j][k]表示用第一个串的前i隔字符和第二个串的前k隔字符组成长度为i的串的个数,那么:若s1[j+1] == s2[k+1] dp[i+1][j+1][k+1] += ...
- POJ 2406
思路:由于题目要求的是最大值,因此从n开始向下查找,第一次出现的满足条件的那个数就是最大的,查找就可以结束,如果查找到1是仍未找到合适的值,则为1,就是说不是任何字符串的次方如abcd #includ ...
- ActiveMQ中的安全机制 [转]
本文简单介绍ActiveMQ通过JAAS实现的安全机制.JAAS(Java Authentication and Authorization Service)也就是java认证/授权服务.这是两种不同 ...
- Windows下Lua+Redis 断点调试环境搭建==Linux下类似
Lua+Redis 断点调试环境搭建 windows环境,使用Redis,写lua脚本头疼的问题之一不能对脚本断点调试,google加上自己的摸索,终于搞定. 1.下载ZeroBraneStudio, ...
- 【HTML】Beginner7:Image
1.Image The web is not just about text,it is a multi-media extravaganza and the most common form ...
- Java笔记(九)……面向对象I
面向对象概念 理解面向对象 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程 强调的是功能行为 面向对象 将功能封装进对象,强调具备了功能的对象. 面向对象是基于面向过程的. 面向 ...