`
weiweirenhong
  • 浏览: 32578 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

Intents and Intent Filters---Intent Resolution

阅读更多
Intent Resolution

Intents 可以分成两类:

    1、Explicit intents (显式intents-指定了目标组件名) designate the target component by its name (the component name field, mentioned earlier, has a value set). Since component names would generally not be known to developers of other applications, explicit intents are typically used for application-internal messages — such as an activity starting a subordinate service or launching a sister activity.【翻译:显式intent通常是用于在一个应用中需要激活某个activity的时候使用。】

    2、Implicit intents(隐式intents-没有指定目标组件名) do not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications.【翻译:隐式intent通常是在需要激活不同应用之间的某个activity的时候使用。】

   Android delivers an explicit intent to an instance of the designated target class. Nothing in the Intent object other than the component name matters for determining which component should get the intent.【翻译:Android系统在判定哪个组件响应一个显式intent的时候,intent实例中的组件名属性是最为关键的匹配判定依据】

   A different strategy is needed for implicit intents. In the absence of a designated target, the Android system must find the best component (or components) to handle the intent — a single activity or service to perform the requested action or the set of broadcast receivers to respond to the broadcast announcement. It does so by comparing the contents of the Intent object to intent filters, structures associated with components that can potentially receive intents. Filters advertise the capabilities of a component and delimit the intents it can handle. They open the component to the possibility of receiving implicit intents of the advertised type. If a component does not have any intent filters, it can receive only explicit intents. A component with filters can receive both explicit and implicit intents.

翻译:由于隐式intent没有指定目标组件名,这时候Android系统判定目标组件需要一个不同的匹配判定策略,借助该策略来告知 Android系统哪个最为适合的组件来响应该隐式intent,这个基本的判定策略是把当前intent内容与当前应用的manifest文件中的所有 intent过滤器做比对,intent过滤器的作用是声明某个组件所能响应的隐式intent类型。如果manifest文件中的组件声明中没有定义 intent过滤器,那么该组件只能响应显式intent;如果一个组件声明中定义了intent过滤器,那么该组件既可以响应隐式intent也可以响应显式intent。

Only three aspects of an Intent object are consulted when the object is tested against an intent filter:【翻译:针对隐式intent的判定策略只对intent实例中以下三类特征信息来和相关的过滤器做比对】

action
data (both URI and data type)
category


The extras and flags play no part in resolving which component receives an intent.( 翻译:extras和flags不是Android系统判定哪个目标组件响应隐式intent的依据。)

需要说明的是:显式intent依然是可以使用extras和flags属性来向目标组件传递数据和向Android 系统发送启动、管理目标组件的方式、方法指令的。换句话说,如果你是使用显式intent实例来激活目标组件(如activity)的时候,首先需要给你的intent设置目标组件名或在构造intent实例时候直接指定目标组件类名(称之为显式intent);然后,如果目标组件需要接受数据,就用 intent.putExtra方法设置要传递的数据;如果说你希望进一步通过当前intent实例告知运行环境系统如何创建或管理目标组件实例的话,就使用 intent.setFlags(flags)方法设置相应的控制指令。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics