【Android】Intent解读
Intent 的作用
Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯。
比如说调用startActivity()来启动一个activity,或者由broadcaseIntent()来传递给所有感兴趣的BroadcaseReceiver, 再或者由startService()/bindservice()来启动一个后台的service。所以可以看出来,intent主要是用来启动其他的activity 或者service,所以可以将intent理解成activity之间的粘合剂。
Intent的构成
要在不同的activity之间传递数据,就要在intent中包含相应的东西。
一般来说数据中最基本的应该包括:
- Action:用来指明要实施的动作是什么,比如说ACTION_VIEW, ACTION_EDIT等。具体的可以查阅android SDK-> reference中的Android.content.intent类,里面的constants中定义了所有的action。
- Data:要实施的具体的数据,一般由一个Uri变量来表示。
下面是一些简单的例子:
ACTION_VIEW content://contacts/1 //显示identifier为1的联系人的信息。
ACTION_DIAL content://contacts/1 //给这个联系人打电话
除了Action和data这两个最基本的元素外,intent还包括一些其他的元素,
Category(类别):这个选项指定了将要执行的这个action的其他一些额外的信息,例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。具体同样可以参考android SDK-> reference中的Android.content.intent类。
Type(数据类型): 显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
component(组件): 指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。
extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。
下面是这些额外属性的几个例子:
ACTION_MAIN with category CATEGORY_HOME //用来 Launch home screen
ACTION_GET_CONTENT with MIME type vnd.android.cursor.item/phone //用来列出列表中的所有人的电话号码
综上可以看出,action、 data/type、category和extras 一起形成了一种语言,这种语言可以是android可以表达出诸如“给张三打电话”之类的短语组合。
intent的解析
应用程序的组件为了告诉Android自己能响应、处理哪些隐式Intent请求,可以声明一个甚至多个Intent Filter。
每个Intent Filter描述该组件所能响应Intent请求的能力——组件希望接收什么类型的请求行为,什么类型的请求数据。
比如请求网页浏览器,网页浏览器程序的Intent Filter就应该声明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及与之相关的请求数据是网页地址URI格式。
如何为组件声明自己的Intent Filter? 常见的方法是在AndroidManifest.xml文件中用属性< Intent-Filter>描述组件的Intent Filter。
隐式Intent(Explicit Intents)和Intent Filter(Implicit Intents)进行比较时的三要素是Intent的动作、数据以及类别。
实际上,一个隐式Intent请求要能够传递给目标组件,必要通过这三个方面的检查。如果任何一方面不匹配,Android都不会将该隐式Intent传递给目标组件。
接下来看看这三方面检查的具体规则。
动作测试
<intent-filter>元素中可以包括子元素< action>,比如:
< intent-filter>
< action android:name=”com.example.project.SHOW_CURRENT” />
< action android:name=”com.example.project.SHOW_RECENT” />
< action android:name=”com.example.project.SHOW_PENDING” />
< /intent-filter>
一条< intent-filter>元素至少应该包含一个< action>,否则任何Intent请求都不能和该< intent-filter>匹配。
如果Intent请求的Action和< intent-filter>中个某一条< action>匹配,那么该Intent就通过了这条<intent-filter>的动作测试。
如果Intent请求或<intent-filter>中没有说明具体的Action类型,那么会出现下面两种情况:
- 如果< intent-filter>中没有包含任何Action类型,那么无论什么Intent请求都无法和这条< intent- filter>匹配;
- 反之,如果Intent请求中没有设定Action类型,那么只要< intent-filter>中包含有Action类型,这个 Intent请求就将顺利地通过< intent-filter>的行为测试。
类别测试
<intent-filter>元素可以包含< category>子元素,比如:
< intent-filter >
< category android:name=”android.Intent.Category.DEFAULT” />
< category android:name=”android.Intent.Category.BROWSABLE” />
< /intent-filter>
只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请求通过测试,IntentFilter中多余的< category>声明并不会导致匹配失败。
一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没有设置类别的Intent请求。
数据测试
数据在<intent-filter>中的描述如下:
< intent-filter >
< data android:type=”video/mpeg” android:scheme=”http” />
< data android:type=”audio/mpeg” android:scheme=”http” />
< /intent-filter>
元素指定了希望接受的Intent请求的数据URI和数据类型,URI被分成三部分来进行匹配:scheme、 authority和path。
其中,用setData()设定的Intent请求的URI数据类型和scheme必须与IntentFilter中所指定的一致。
若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。
简单例子说明
使用Intent激活Android自带的电话拨号程序,通过这个实例会发现,使用Intent并不像其概念描述得那样难。
最终创建Intent的代码如下所示。
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));
创建好Intent之后,就可以通过它告诉Android希望启动新的Activity了。
startActivity(i);
Intent的构造函数
公共构造函数:
- Intent() //空构造函数
- Intent(Intent o) //拷贝构造函数
- Intent(String action) //指定action类型的构造函数
- Intent(String action, Uri uri) //指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider
- Intent(Context packageContext, Class<?> cls) //传入组件的构造函数,也就是上文提到的
- Intent(String action, Uri uri, Context packageContext, Class<?> cls) //前两种结合体
Intent有六种构造函数,3、4、5是最常用的!
Intent(String action, Uri uri) 的action就是对应在AndroidMainfest.xml中的action节点的name属性值。
在Intent类中定义了很多的Action和Category常量。
示例代码
Intent intent = new Intent(Intent.ACTION_EDIT, null);
startActivity(intent);
示例代码用了第四种构造函数,只是uri参数为null。
执行此代码的时候,系统就会在程序主配置文件AndroidMainfest.xml中寻找<action android:name="android.intent.action.EDIT" />对应的Activity,如果对应为多个activity具有<action android:name="android.intent.action.EDIT" />此时就会弹出一个dailog选择Activity。
利用Intent在Activity之间传递数据
在Main中执行如下代码:
Bundle bundle = new Bundle();
bundle.putStringArray("NAMEARR", nameArr);
Intent intent = new Intent(Main.this, CountList.class);
intent.putExtras(bundle);
startActivity(intent);
在CountList中,代码如下:
Bundle bundle = this.getIntent().getExtras();
String[] arrName = bundle.getStringArray("NAMEARR");
以上代码就实现了Activity之间的数据传递!
实例总结说明
用获取到的Intent直接调用startActivity(returnIt)就ok了。
调用web浏览器
Uri myBlogUri = Uri.parse("http://lcw.cnblogs.com");
returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);
地图
Uri mapUri = Uri.parse("geo:38.899533,-77.036476");
returnIt = new Intent(Intent.ACTION_VIEW, mapUri);
调拨打电话界面
Uri telUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_DIAL, telUri);
卸载
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
安装
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
播放
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
调用发邮件
Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com");
returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);
发短信
Uri smsUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_VIEW, smsUri);
returnIt.putExtra("sms_body", "hello,lcw");
returnIt.setType("vnd.android-dir/mms-sms");
直接发邮件
Uri smsToUri = Uri.parse("smsto://100861");
returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);
returnIt.putExtra("sms_body", "hello,lcw");
发彩信
Uri mmsUri = Uri.parse("content://media/external/images/media/23");
returnIt = new Intent(Intent.ACTION_SEND);
returnIt.putExtra("sms_body", "hello,lcw");
returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);
returnIt.setType("image/png");
【Android】Intent解读的更多相关文章
- android Intent介绍
Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...
- android:Intent匹配action,category和data原则
1.当你在androidmanifest里面定义了一个或多个action时 你使用隐式意图其他activity或者service时,规定你隐式里面的action必须匹配XML中定义的action,可以 ...
- Android Intent
Intent在Android中的重要性不言而喻.本文主要总结下Intent使用过程中需要注意的一些问题. 1.隐式Intent AndroidManifest.xml声明时<intent-fil ...
- android intent和intent action大全
1.Intent的用法:(1)用Action跳转1,使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个 Activity的IntentFilter段中 定义了包含了 ...
- Android总结篇系列:Android Intent
Intent在Android中的重要性不言而喻.本文主要总结下Intent使用过程中需要注意的一些问题. 1.隐式Intent AndroidManifest.xml声明时<intent-fil ...
- 什么时候加上android.intent.category.DEFAULT
什么时候加上android.intent.category.DEFAULT 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent ...
- (转)android.intent.action.MAIN与android.intent.category.LAUNCHER
android.intent.action.MAIN决定应用程序最先启动的Activity android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里 在网上看到 ...
- android之android.intent.category.DEFAULT的用途和使用
1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...
- 理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER
刚才看了一下sundy的视频<LLY110426_Android应用程序启动>,里面讲到luncher这个activity通过获取应用程序信息来加载应用程序,显示给用户,其中就是通过一个应 ...
随机推荐
- perf之record
如果CPU的使用率突然暴涨,如何迅速定位是哪个进程.哪段代码引起的呢?我们需要一个profiling工具,对CPU上执行的代码进行采样.统计,告诉我们CPU到底在忙些什么. perf 就是这样的工具. ...
- std::thread 不 join
std::thread 构造之后 使用 detach.就可以了
- 【LeetCode】207. Course Schedule (2 solutions)
Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some cours ...
- Zabbix触发器支持的函数说明
原文出处:https://www.zabbix.com/documentation/2.0/manual/appendix/triggers/functions 译者: pengyao abschan ...
- 自由是有代价的:聊聊这几年尝试的道路 要想生活好,别看哲学书和思想书。简单看看可以,看多了问题就大了。还是要去研究研究些具体的问题。别jb坐在屋子里,嘴里念着海子的诗,脑袋里想康德想的事情,兜里屁都没有,幻想自己是大国总理,去想影帝是怎么炼成的。
自由是有代价的:聊聊这几年尝试的道路 现在不愿意写过多的技术文章了,一点是现在做的技术比较偏,写出来看的人也不多,二来是家庭事务比较繁多,没以前那么有时间写了.最近,园子里多了一些写经历的文章,我也将 ...
- Eclipse的数据库插件
今天上午升级 Eclipse 到 3.1.2 版本,完了之后就想找个数据库的插件,但花了近 2 个小时后得出的结论是:还没有支持 3.1.x 版本的数据库插件,郁闷的不行.看看 eclipse3.1. ...
- STM32的JTAG下载模式
SWJ:串行线JTAG配置 (Serial wire JTAG configuration) SWJ(串行线JTAG)支持JTAG或SWD访问Cortex的调试端口. 系统复位后的默认状态是启用SW ...
- Java – How to add days to current date
1. Calendar.add Example to add 1 year, 1 month, 1 day, 1 hour, 1 minute and 1 second to the current ...
- css ::selection 的妙用
1.选中页面文字和元素时的背景颜色 ::selection { background: #25b864; color: #fff; } 2.不能选择页面内容(但可以拖拽内容进行复制.挺好玩的) ::s ...
- C#基础课程之四集合(ArrayList、List<泛型>)
list泛型的使用 ArrayList list = new ArrayList(); ArrayList list = ); //可变数组 list.Add("我"); //Ad ...