Android学习之Intent机制
来源:互联网 发布:惠普2025网络设置 编辑:程序博客网 时间:2024/04/28 09:43
在一个Android应用中,主要是由四种组件组成的,这四种组件可参考“Android应用的构成”。而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用。在这些组件之间的通讯中,主要是由Intent协助完成的。
· Activity:一个intent对象传递给Context.startActivity()或Activity.startActivityForRestult()去启动一个活动或使一个已存在的活动去做新的事情。
· Service:一个intent对象传递给Context.startService()去初始化一个service或传递一个新的指令给正在运行的service。类似的,一个intent可以传递给Context.bindService()去建立调用组件和目标服务之间的连接。
· Broadcast Receiver:一个intent对象传递给任何广播方法(如Context.sendBroadcast(),Context.sendOrderedBroadcast(),Context.sendStickyBroadcast()),都将传递到所有感兴趣的广播接收者。 在每种情况下,Android系统查找合适的activity、service、broadcastreceivers来响应意图,如果有必要的话,初始化他们。这些消息系统之间没有重叠,即广播意图仅会传递给广播接收者,而不会传递活动或服务,反之亦然。
Constant--Target component--Action
ACTION_CALL--activity--Initiate a phone call.Initiate a phone call.
ACTION_EDIT--activity--Display data for the user to edit.
ACTION_MAIN--activity--Start up as the initial activity of a task, with no data input and no returned output.
ACTION_SYNC--activity--Synchronize data on a server with data on the mobile device.
ACTION_BATTERY_LOW--broadcast receiver--A warning that the battery is low.
ACTION_HEADSET_PLUG--broadcast receiver--A headset has been plugged into the device, or unplugged from it.
ACTION_SCREEN_ON--broadcast receiver--The screen has been turned on.
ACTION_TIMEZONE_CHANGED--broadcast receiver--The setting for the time zone has changed.
Constant--Meaning
CATEGORY_BROWSABLE--The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.
CATEGORY_GADGET--The activity can be embedded inside of another activity that hosts gadgets.
CATEGORY_HOME--The activity displays the home screen, the first screen the user sees when the device is turned on or when-the HOME key is pressed.
CATEGORY_LAUNCHER--The activity can be the initial activity of a task and is listed in the top-level application launcher.
CATEGORY_PREFERENCE--The target activity is a preference panel.
额外的键值对信息应该传递到组件处理intent。就像动作关联的特定种类的数据URIs,也关联到某些特定的附加信息。例如,一个ACTION_TIMEZONE_CHANGEintent有一个"time-zone"的附加信息,标识新的时区,ACTION_HEADSET_PLUG有一个"state"附加信息,标识头部现在是否塞满或未塞满;有一个"name"附加信息,标识头部的类型。如果你自定义了一个SHOW_COLOR动作,颜色值将可以设置在附加的键值对中。
· 显式intent:通过名字指定目标组件。因为开发者通常不知道其它应用程序的组件名字,显式intent通常用于应用程序内部消息,如一个活动启动从属的服务或启动一个姐妹活动。
· 隐式intent:并不指定目标的名字(组件名字字段是空的)。隐式intent经常用于激活其它应用程序中的组件。
隐式intent需要另外一种不同的策略。由于缺省指定目标,Android系统必须查找一个最适合的组件(一些组件)去处理intent——一个活动或服务去执行请求动作,或一组广播接收者去响应广播声明。这是通过比较Intent对象的内容和intent过滤器(intentfilters)来完成的。intent过滤器关联到潜在的接收intent的组件。过滤器声明组件的能力和界定它能处理的intents,它们打开组件接收声明的intent类型的隐式intents。如果一个组件没有任何intent过滤器,它仅能接收显示的intents,而声明了intent过滤器的组件可以接收显示和隐式的intents。
- <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对象没有指定动作,将自动通过检查(只要过滤器至少有一个过滤器,否则就是上面的情况了)
- <intent-filter . . . >
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.BROWSABLE" />
- .. .
- </intent-filter>
注意:"android.intent.action.MAIN"和 "android.intent.category.LAUNCHER"设置,它们分别标记活动开始新的任务和带到启动列表界面。它们可以包含"android.intent.category.DEFAULT"到种类列表,也可以不包含。
(1.3)数据检测
- <intent-filter . . . >
- <data android:mimeType="video/mpeg" android:scheme="http" . . . />
- <data android:mimeType="audio/mpeg" android:scheme="http" . . . />
- . . .
- </intent-filter>
每个<data>元素指定一个URI和数据类型(MIME类型)。它有四个属性scheme、host、port、path对应于URI的每个部分:
scheme://host:port/path
例如,下面的URI:
content://com.example.project:200/folder/subfolder/etc
scheme是content,host是"com.example.project",port是200,path 是"folder/subfolder/etc"。host和port一起构成URI的凭据(authority),如果host没有指定,port也被 忽略。 这四个属性都是可选的,但它们之间并不都是完全独立的。要让authority有意义,scheme必须也要指定。要让path有意义,scheme和authority也都必须要指定。
· 一个Intent对象既不包含URI,也不包含数据类型:仅当过滤器也不指定任何URIs和数据类型时,才不能通过检测;否则都能通过。
· 一个Intent对象包含URI,但不包含数据类型:仅当过滤器也不指定数据类型,同时它们的URI匹配,才能通过检测。例如,mailto:和tel:都不指定实际数据。
· 一个Intent对象包含数据类型,但不包含URI:仅当过滤也只包含数据类型且与Intent相同,才通过检测。
· 一个Intent对象既包含URI,也包含数据类型(或数据类型能够从URI推断出):数据类型部分,只有与过滤器中之一匹配才算通过;URI部分,它的URI要出现在过滤器中,或者它有content:或file: URI,又或者过滤器没有指定URI。换句话说,如果它的过滤器仅列出了数据类型,组件假定支持content:和file: 。
上面最后一条规则表明组件能够从文件或内容提供者获取本地数据。因此,它们的过滤器仅列出数据类型且不必明确指出content:和file: scheme的名字。这是一种典型的情况,一个<data>元素像下面这样:
- <data android:mimeType="image/*" />
- <data android:scheme="http" android:type="video/*" />
- <intent-filter . . . >
- <action android:name="codeandroid.intent.action.MAIN" /> <category android:name="codeandroid.intent.category.LAUNCHER" />
- </intent-filter>
- Android学习之Intent机制
- Android Intent 机制学习
- Android意图机制之Intent
- android之Intent机制详解
- Android之Intent学习
- Android学习之Intent
- android学习笔记之Intent&Intent filter
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- JAVA利用JDBC对数据库的操作和JDBC编程之事务处理
- UDP-Java-客户机/服务器应用程序
- ironic简介
- 如何给SolidWorks工程图添加条型码?
- c++考悟
- Android学习之Intent机制
- vmware三种连接方式Bridge、Host-Only模式和NAT模式
- 常见颜色码值查询
- C++难呀。。。。
- 文本聚类算法分析
- 建议大哥应该精通一门,比如C++
- 1.3000小时左右的自学时间??就算一天1小时
- IOS UI动画与手势的基本使用
- Docker背后的内核知识——Namespace资源隔离