您的位置:首页 > 其它

Intents and IntentFilters

2012-11-02 14:17 375 查看
 

Intents and IntentFilters

startActivity()
 

startActivityForResult()
 

startService()
 

bindService()
 

sendBroadcast()
,

sendOrderedBroadcast()

sendStickyBroadcast()

 

函数执行后,android系统会自动将该Intent传递给合适的Activity   service broadcast receivers并对传递过来的Intent发生响应。调用与响应不会发生交叉,即一个传入
startActivity()
 的Intent只会传递给一个Activity,而不会传递给service和
broadcast receivers。

 

Intent对象是一个信息的载体,包含以下属性

Component name

处理意图请求的组件名称,该字段是一个 
ComponentName
 类型的对象,包含target
component,例如:com.example.project.app.FreneticActivity

 package name 例如com.example.project

 

该参数是可选的,设置了则会将Intent传递给对应的class,没有设置则利用 Intent对象的其他信息找到合适的目标组件

 

Component name可以有 
setComponent()
setClass()
,
or 
setClassName()
设置


 


Action

A string naming the actionto be performed ,Intent类中定义了一些action常量,例如

Constant
Target component
Action
ACTION_CALL
activity
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.
 

如果自定义action strings常量,需要加上包名做为前缀,例如

"
com.example.project.SHOW_COLOR
"

 

 

Intent 对象中的action属性由 
setAction()
方法设置


 


 

Data

The URI of the data to beacted on and the MIME type of that data. Different actions are paired withdifferent kinds of data specifications. For example, if the action field is 
ACTION_EDIT
,
the data field would contain the URI of the document to bedisplayed for editing. If the action is 
ACTION_CALL
, the data
field would be a 
tel:
 URI with the number to call. Similarly,
if the action is 
ACTION_VIEW
 and the data field is an 
http:
 URI,
the receiving activity would be called upon to download anddisplay whatever data the URI refers to.

 

Extras

Key-value pairs foradditional information that should be delivered to the component handling theintent. 

 

Flags

 

 

Intent可以分为两种类型

一种是显示的指定componentname一种是隐式意图,不明确设置componentname的值,因为如果一个应用的某个activity想指定另外一个应用的activity或者service并不会知道另外一个应用的activity名称,因此显示的Intent一般用来应用内部的activity切换数据传递。

而隐式的Intent常用来启动其他application的components

 

Android delivers anexplicit intent to an instance of the designated target class. Nothing in theIntent object other than thecomponent
name matters fordetermining which component should get the intent.

对于一个显式的Intent传递,只有component name能够决定哪个component接受该Intent

 

对于隐式Intent,没有明确的指明哪个类来接受Intent,androdi系统会将Intent
Object与

intentfilters比较选择最佳的component来接受该Intent并对其响应。

 

Intent filters标示了一个component能够处理的Intent。如果一个activity中没有生明intent
filters,那么该activity只能接受显示的的Intent。如果声明了,那么显示的隐式的Intent均可以接受。

 

Only three aspects of an Intent objectare consulted when the object is tested against an intent filter:
action 

data (both URI and data type) 

category

 

 

Intent filters

To inform the system whichimplicit intents they can handle, activities, services, and broadcast receiverscan have one or more intent filters. Each filter describes a capability of thecomponent, a set of intents
that the component is willing to receive.

 

 An explicit intentis always delivered to its target, no matter what it contains; the filter isnot consulted. But an implicit intent is delivered to a
component only if itcan pass through one of the component's filters.

 

An intent filter is aninstance of the 
IntentFilter
 class.
However, since the Android system must know about thecapabilities of a component before it can launch that component, intent filtersare generally not set up in Java code, but in the application's manifest file(AndroidManifest.xml) as 
<intent-filter>
 elements.
(The one exception would be filters for broadcast receiversthat are registered dynamically by calling
Context.registerReceiver()
;
they are directly created as IntentFilter objects.)

 

Action test

 

To pass this test, the action specified in the Intentobject must match one of the actions listed in the filter. If the object or thefilter does not specify an action, the results are as follows:
·                       If the filter fails tolist any actions, there is nothing for an intent to match, so all intents failthe test. No intents can get through the filter.
·                       On the other hand, anIntent object that doesn't specify an action automatically passes the test — aslong as the filter contains at least one action.
 

Category test

 

In principle, therefore, anIntent object with no categories should always pass this test, regardless ofwhat's in the filter. That's mostly true. However, with one exception, Androidtreats all implicit intents
passed to
startActivity()
 as
if they contained at least one category: "
android.intent.category.DEFAULT
" (the 
CATEGORY_DEFAULT
 constant).
Therefore, activities that are willing to receive implicitintents must include "
android.intent.category.DEFAULT
" in
their intent filters. (Filters with "
android.intent.action.MAIN
" and "
android.intent.category.LAUNCHER
"
settings are the exception. They mark activities thatbegin new tasks and that are represented on the launcher screen. They caninclude "
android.intent.category.DEFAULT
"
in the list of categories, but don't need to.) See Using intentmatching,
later, for more on these filters.)

 

Data test

 







Because the system runs each application in a separate process with
file permissions that restrict access to other applications, your application cannot directly activate a component from another application. The Android system, however, can. So, to activate a component in another application, you must deliver a message to
the system that specifies your intent to
start a particular component. The system then activates the component for you.


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: