Android笔记一.深入理解Intent和IntentFilters(一)
和 Data)和Android 系统关心的信息(如Category等)。
也就是说,发送"意图"的组件通过Intent对象所包括的内容。来启动指定的(即Component属性)或通过筛选(即Action&Category属性)的某(些)组件,然后实施对应的动作(即Action属性)并传递对应的数据(即Data属性)以便完毕对应的动作。
二、Intent是怎样实现组件间相互调用?
Filters进行匹配(假设带有Component信息,就不用比了)。假设找到了,就把相关Component的信息通知回AcitivityManagerService,在这里。会完毕启动Activity这个非常多细节的事情。
2.Intent匹配
Filters/>元素,它包括了<action/>、<data/>等子元素。当我们的intent对象没有包括Component信息时,这样的"意图"被称之为隐形"意图"。也就是说,"意图"没有指明详细要启动哪个组件以及完毕什么样的动作。
这时我们就须要通过Intent
Filters中的子元素进行信息匹配。从而确定当前包括Intent Filters属性的Activity是不是我们要启动的那个(些)组件。即发送"意图"组件配置好intent对象,被启动组件实现Intent Filters属性,最后。发送组件会依据被启动组件AndroidMainFest.xml中的<Intent
Filters/>信息来确定它是不是目标组件。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="446" height="215" alt="" style="background-color:inherit; display:inline-block; margin-top:8px; width:446.12500000000006px; height:215px">
三、Intent对象具体解释
Intent代表了Android应用的启动"意图"。Android应用将会依据Intent来启动指定组件,至于究竟启动哪个组件,则取决于Intent的属性。
1.Action属性
(1)自己定义字符串
public final String CUSTOME_ACTION="intent.action.CUSTOME_JIANG";//字符串能够随意
Intent intent=new Intent();
intent.setAction(ActionAttr.CUSTOME_ACTION); //注意:ActionAttr为我们创建的类,也能够使用this.CUSTOME_ACTION
(2)使用系统预定action常量
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL); //当中ACTION_CALL为Intent类的静态成员变量,能够类直接调用
//相应字符串"android.intent.action.CALL"
2.Data属性
这里须要注意的是,Data属性仅仅接受一个Uri对象,一个Uri对象通常通过例如以下形式的字符串来表示:
Intent intent=new Intent();
String data="content://com.android.contacts/contacts/1";
Uri uri=Uri.parse(data);//将字符串转换为Uri
intent.setData(uri);
或者
Intent intent=new Intent();
intent.setData(Uri.parse("content://com.android.contacts/contacts/1"));
ACTION_VIEW content://contacts/N } 传递到一个新的Activity。
3.Catagory属性
Category属性能够自己定义字符串实现。但为了方便不同应用之间的通信还能够设置系统提前定义的Category常量。
调用方法addCategory
用来为Intent 加入一个Category。方法removeCategory 用来移除一个Category;getCategories方法返回已定义的Category。
(1)自己定义字符串
public final String CUSTOME_CATEGORY="intent.action.CUSTOME_CATEGORY";//字符串能够随意
Intent intent=new Intent();
intent.addCategory(ActionAttr.CUSTOME_CATEGORY);
(2)使用系统预定action、category常量
下面代码实现当点击某个button时,通过Intent对象实现返回HOME桌面。 Intent intent=new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);//返回Home桌面
博主笔记2:一般来说Action属性和Category属性都是同一时候使用的。
在Android中,全部应用主Activity(就是单独启动时候。第一个执行的那个Activity...)。都须要能够接受一个Category为 CATEGORY_LAUNCHER,Action为ACTION_Main的意图。
对于发出"意图"的组件。我们能够通过setAction()、addCategory()方法为"意图"加入action属性或者同一时候加入Action、Category属性;对于接收"意图"的组件,在AndroidManifest.xm文件里,我们能够这样设置:
/> //默认Action
/> //默认Category
/> //默认Category
另外,假设我们使用了系统提前定义的action常量,则须要在AndroidManifest.xm文件里加入对应的权限,这方面的内容我们将在第二部分讲到。
但事实上不然,Type信息。是用MIME来表示的,比方text/plain。这种东西。
讲到这里。两者区别就非常清晰了,Data就是门牌号,指明了详细的位置。详细问题详细分析。而type,则是强调物以类聚,解决一批量的问题。实际的样例是这种。比方,从某个应用拨打一个电话,会发起的是action为ACTION_DIAL且data为tel:xxx这种Intent。相应的人类语言就是拨打xxx的电话。非常具象。而假设使用type,就宽泛了很多,比方浏览器收到一个未知的MIME类型的数据(比方一个视频...),就会放出这种Intent。求系统的其它应用来帮助,表达成自然语言应该就是:查看pdf类文档,这种。
博主笔记3:MIME类型?
MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被訪问的时候,浏览器会自己主动使用指定应用程序来打开。多用于指定一些client自己定义的文件名称。以及一些媒体文件打开方式。
最早的HTTP协议中,并没有附加的数据类型信息,全部传送的数据都被客户程序解释为超文本标记语言HTML
文档,而为了支持多媒体数据类型,HTTP协议中就使用了附加在文档之前的MIME数据类型信息来标识数据类型。
MIME意为多功能Internet邮件扩展,它设计的最初目的是为了在发送电子邮件时附加多媒体数据,让邮件客户程序能依据其类型进行处理。
然而当它被HTTP协议支持之后。它的意义就更为显著了。
它使得HTTP传输的不仅是普通的文本,而变得丰富多彩。每一个MIME类型由两部分组成,前面是数据的大类别,比如声音audio、图象image等,后面定义详细的种类。
5.Ertras属性
类的对象,有一组可序列化的key/value对组成。
每个Action都会有与之相应的key和value类型约定。发起Intent的时候,须要依照要求把Data不能表示的额外參数放入Extras中。
6.Flags属性
位构成,这些标志,是用来指明执行模式的。
比方,你期望这个意图的执行者,和你执行在两个全然不同的任务中(或说进程也无妨吧...),就须要设置FLAG_ACTIVITY_NEW_TASK 的标志位。
Intent Filters它是用来描写叙述一个Activity或 Serveice等组件,我们通过在组件AndroidManifest.xml中<intent-ilters/>元素中加入<action/>等属性。以满足期望可以响应怎么样的Intent,这样的没有指明要启动组件名方式就称之为隐式intent。
当然,我们也可以使"意图"实现启动指定的组件,即称之为显示intent,主要通过Component属性来实现。Intent的Component属性须要接受一个ComponentName对象,这个对实现将要启动指定组件的类名、类所在的包名绑定在intent上。
ComponentName comp=new ComponentName(ComponentAttr.this,SecondaryActivity.class);
Intent intent=new Intent();
intent.setComponent( comp);//设置intent的Component属性,指定"意图"要启动组件的包和类名
凝视:第一句用于创建一个ComponentName对象,来指定包名和类型-这就能够唯一地确定一个组件类。
四、Intent相关类
void |
startActivity(Intent intent)
作用:启动Activity,详细启动哪个Activity和怎么样启动由intent属性决定
|
void |
startActivityForResult(Intent
intent, int requestCode) 作用:启动Activity,并返回一个结果。当被启动的Activity退出时。会调用 onActivityResult() 方法并向其传入一个 requestCode參数,这个 requestCode參数为非负数(>0)。作用是标志是哪个Activity组件发出的"意图",须要注意的是假设 requestCode小于0时,这种方法的仅仅能用于启动一个Activity而不能返回值了。
另外,Intent的action属性设为可以返回一个结果,假设设置为 |
|
|
|
|
|
|
|
|
|
为一个指定的组件创建一个带action和data属性的意图
|
(2)经常用法
addCategory(String category)
Add a new category to the intent.
|
|
addFlags(int
flags) Add additional flags to the intent (or with existing flags value).
|
|
Retrieve the general action to be performed, such as
ACTION_VIEW . |
|
Return the set of all categories in the intent.
|
|
Retrieve the concrete component associated with the intent.
|
|
getData()
Retrieve data this intent is operating on.
|
|
Retrieves a map of extended data from the intent.
|
|
int
|
getFlags()
Retrieve any special flags associated with this intent.
|
static Intent
|
This method was deprecated in API level 4. Use
parseUri(String, instead. |
Retrieve the application package name this Intent is limited to.
|
|
Return the scheme portion of the intent's data.
|
|
Return the specific selector associated with this Intent.
|
|
Get the bounds of the sender of this intent, in screen coordinates.
|
|
getType()
Retrieve any explicit MIME type included in the intent.
|
|
boolean
|
hasCategory(String category)
Check if a category exists in the intent.
|
boolean
|
Returns true if an extra value is associated with the given name.
|
static Intent
|
makeMainActivity(ComponentName mainActivity)
Create an intent to launch the main (root) activity of a task.
|
static Intent
|
Make an Intent for the main activity of an application, without specifying a specific activity to run but giving a selector to find the
activity. |
static Intent
|
Parses(解析) the "intent" element (and its children) from XML and instantiates an Intent object.
|
static Intent
|
Create an intent from a URI.
|
Add extended data to the intent.
|
|
Add extended data to the intent.
|
|
Set the general action to be performed.
|
|
>)" style="background-color:inherit; color:rgb(37,138,175)">setClass (Context packageContext, Class<?>cls) |
|
Convenience for calling
setComponent(ComponentName) withan explicit class name. |
|
Convenience for calling
setComponent(ComponentName) withan explicit application package name and class name. |
|
setComponent(ComponentName component)
(Usually optional) Explicitly set the component to handle the intent.
|
|
Set the data this intent is operating on.
|
|
setFlags(int
flags) Set special flags controlling how this intent is handled.
|
|
setPackage(String packageName)
(Usually optional) Set an explicit application package name that limits the components this Intent will resolve to.
|
|
void
|
Set the bounds of the sender of this intent, in screen coordinates.
|
Set an explicit MIME data type.
|
|
toString()
Returns a string containing a concise(简洁), human-readable (可读)description of this object.
|
|
toUri(int
flags) Convert this Intent into a String holding a URI representation of it.
|
(3)类静态成员变量
ACTION_MAIN
//传递返回到主Activity动作ACTION_VIEW
//传递显示动作ACTION_ATTACH_DATA
ACTION_EDIT
//传递编辑动作ACTION_PICK
ACTION_CHOOSER
//传递选择动作ACTION_GET_CONTENT
ACTION_DIAL
ACTION_CALL
ACTION_SEND
ACTION_SENDTO
ACTION_ANSWER
//传递接听电话动作ACTION_INSERT
ACTION_DELETE
ACTION_RUN
ACTION_SYNC
ACTION_PICK_ACTIVITY
ACTION_SEARCH
ACTION_WEB_SEARCH
ACTION_FACTORY_TEST
b.标准Broadcast Actions
文件里定义了<receiver>属性的Activity。
ACTION_TIME_TICK
ACTION_TIME_CHANGED
ACTION_TIMEZONE_CHANGED
ACTION_BOOT_COMPLETED
ACTION_PACKAGE_ADDED
ACTION_PACKAGE_CHANGED
ACTION_PACKAGE_REMOVED
ACTION_PACKAGE_RESTARTED
ACTION_PACKAGE_DATA_CLEARED
ACTION_UID_REMOVED
ACTION_BATTERY_CHANGED
ACTION_POWER_CONNECTED
ACTION_POWER_DISCONNECTED
ACTION_SHUTDOWN
c.标准Categories常量
d.标准Extra Data常量
EXTRA_ALARM_COUNT
EXTRA_BCC
EXTRA_CC
EXTRA_CHANGED_COMPONENT_NAME
EXTRA_DATA_REMOVED
EXTRA_DOCK_STATE
EXTRA_DOCK_STATE_HE_DESK
EXTRA_DOCK_STATE_LE_DESK
EXTRA_DOCK_STATE_CAR
EXTRA_DOCK_STATE_DESK
EXTRA_DOCK_STATE_UNDOCKED
EXTRA_DONT_KILL_APP
EXTRA_EMAIL
EXTRA_INITIAL_INTENTS
EXTRA_INTENT
EXTRA_KEY_EVENT
EXTRA_ORIGINATING_URI
EXTRA_PHONE_NUMBER
EXTRA_REFERRER
EXTRA_REMOTE_INTENT_TOKEN
EXTRA_REPLACING
EXTRA_SHORTCUT_ICON
EXTRA_SHORTCUT_ICON_RESOURCE
EXTRA_SHORTCUT_INTENT
EXTRA_STREAM
EXTRA_SHORTCUT_NAME
EXTRA_SUBJECT
EXTRA_TEMPLATE
EXTRA_TEXT
EXTRA_TITLE
EXTRA_UID
e.Flags
getFlags()
addFlags(int)
FLAG_GRANT_READ_URI_PERMISSION
FLAG_GRANT_WRITE_URI_PERMISSION
FLAG_GRANT_PERSISTABLE_URI_PERMISSION
FLAG_GRANT_PREFIX_URI_PERMISSION
FLAG_DEBUG_LOG_RESOLUTION
FLAG_FROM_BACKGROUND
FLAG_ACTIVITY_BROUGHT_TO_FRONT
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
FLAG_ACTIVITY_MULTIPLE_TASK
FLAG_ACTIVITY_NEW_DOCUMENT
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_NO_ANIMATION
FLAG_ACTIVITY_NO_HISTORY
FLAG_ACTIVITY_NO_USER_ACTION
FLAG_ACTIVITY_PREVIOUS_IS_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_REORDER_TO_FRONT
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_TASK_ON_HOME
FLAG_RECEIVER_REGISTERED_ONLY
Android笔记一.深入理解Intent和IntentFilters(一)的更多相关文章
- Pro Android 4 第五章 理解Intent
Android引入了一个名为Intent的概念用来唤醒各种组件.Android中的组件包括:activities(UI 组件),services(后台代码),broadcast receiv ...
- Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)
原文:[置顶] Android菜鸟的成长笔记(9)——Intent与Intent Filter(下) 接着上一篇的内容,下面我们再来看看Intent的Data与Type属性. 一.Data属性与Typ ...
- Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)
原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...
- Android学习笔记三:用Intent串联activity
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513399.html 一:Intent Intent可以理解为 意图. 我们可以通过创建intent实例来定义 ...
- [转]android笔记--Intent和IntentFilter详解
Intent用于启动Activity, Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介. 使用Intent启动组件的优势1, Intent为组件的启 ...
- android笔记--Intent和IntentFilter详解
本文转载自:https://www.cnblogs.com/liushengjie/archive/2012/08/30/2663066.html 本文转载自:https://www.cnblogs. ...
- 【转】Android菜单详解——理解android中的Menu--不错
原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...
- 深入理解Intent和IntentFiler(一)
http://blog.csdn.net/u012637501/article/details/41080891 为了比较深刻的理解并灵活使用Intent,我计划将这部分的学习分为两步:一是深入理解I ...
- Android笔记二十七.Service组件入门(一).什么是Service?
转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空) 一.Service 1.Service简单介绍 Service为Android四大组件之中 ...
随机推荐
- MySQL Cluster线上管理节点配置文件-数据节点32G内存
网上的朋友提供的,仅供参考. [NDBD DEFAULT] NoOfReplicas= 2 DataMemory=20G IndexMemory=5G MaxNoOfConcurrentTransac ...
- oracle 之flashback 深入研究。
oracle 之flashback 深入研究. 今天是2013-08-24,开始进行oracle flashback 内部原理研究,记录一下笔记. SQL> startup ORACLE ins ...
- 二叉查找树实现实例(C语言)
/* search_tree.h */ #ifndef _SEARCH_TREE_H #define _SEARCH_TREE_H struct tree_node; typedef struct t ...
- 【二】Drupal 入门之新建主题
Drupal 的模板是以 *.tpl.php 命名的 php 文件 1.在Drupal中,默认模板路径为 moudles/system 这就是我们为什么还没有制作模板 Drupal 就能够正常显示 ...
- FFmpeg源码结构图 - 编码
===================================================== FFmpeg的库函数源码分析文章列表: [架构图] FFmpeg源码结构图 - 解码 FFm ...
- AutoResponder Reference
Fiddler's AutoResponder tab allows you to return files from your local disk instead of transmitting ...
- 解决 Maven was cached in the local repository, resolution will not be reattempted until the update interv
问题原因 Maven默认会使用本地缓存的库来编译工程,对于上次下载失败的库,maven会在~/.m2/repository/<group>/<artifact>/<ver ...
- mysql 运行计划explain具体解释
explain主要是用来获取一个query的运行计划,描写叙述mysql怎样运行查询操作.运行顺序,使用到的索引,以及mysql成功返回结果集须要运行的行数.能够帮助我们分析 select 语句,让我 ...
- 动态布局中RadioGroup的RadioButton有时候不相互排斥的原因
近期在做一个答题类的模块,有单选.简答.调查问卷等,我是用动态布局的方式生成答题项的.在弄单选的时候遇到一个比較奇葩的问题,在代码中生成RadioGroup和RadioButton的时候.会发现不能相 ...
- XML 文档结构必须从头至尾包含在同一个实体内
XML 文档结构必须从头至尾包含在同一个实体内 CreateTime--2018年4月2日16:40:58 Author:Marydon 问题还原: <![CDATA[ <?xml v ...