Android 开发文档 程序基础——Activating components: intents
2010-06-19 10:49
399 查看
前面说的Activities
,Services
,Broadcast receivers
,Content providers
都属于程序组件
Application Components。
而intents则属于激活组件Activating components。
Content
providers在接受到来自ContentResolver的请求时被激活,而其他三个组件则是被异步消息intents激活。Intent是一个持
有消息内容的Intent对象。作用于activities 和
services时,intent命名被请求的动作,指定数据的URI以便操作。例如传达一个请求给activity来呈现一个图片或者编辑文字。作用于
broadcast receivers时,它命名被广播的动作,例如告诉相关的程序照相键被按了。
针对不同的组件有不同的方法来激活:
activity通过传递Intent对象到Context.startActivity()或者
Activity.startActivityForResult()来运行或者执行一些新内容。如果要看正在反应中的activity是由什么
intent引起的,可以调用它的getIntent()。android系统中调用activity的onNewIntent()来传递给之后的
intents。
一个activity经常会导致另一个的运行。如果希望被其运行的activity返回一个结果,那么就必须调用startActivityForResult()而不是
startActivity()。
service的运行或者接受新指令是通过传递intent给
Context.startService()。android调用service的onStart()并传递一个intent对象给它。
类似的,intent可以被传递给
Context.bindService()来确立调用中的组件和目标service之间的联系。service通过onBind()方法中接受
intent对象。如果service没有启动,bindService()可以选择启动它。
程序可以通过传递Intent对象给例如
Context.sendBroadcast()
, Context.sendOrderedBroadcast()
,
Context.sendStickyBroadcast()来初
始化一个broadcast。android会把intent传递给所有相关的broadcast
receivers,通过调用它们的onReceive()。
原文
from a ContentResolver. The other three components — activities,
services, and broadcast receivers — are activated by asynchronous
messages called intents
. An intent is an Intent
object that holds the
content of the message. For activities and services, it names the
action being requested and specifies the URI of the data to act on,
among other things. For example, it might convey a request for an
activity to present an image to the user or let the user edit some text.
For broadcast receivers, the Intent object names the action being
announced. For example, it might announce to interested parties that the
camera button has been pressed.
There are separate methods for activating each type of component:
An activity is launched (or given something new to do) by passing
an Intent object to Context.startActivity()
or Activity.startActivityForResult()
.
The responding activity can look at the initial intent that caused it
to be launched by calling its getIntent()
method. Android calls the activity’s onNewIntent()
method to pass it any subsequent
intents.One activity often starts the next one. If it expects a result
back from the activity it’s starting, it calls startActivityForResult()
instead of startActivity()
.
For example, if it starts an activity that lets the user pick a photo,
it might expect to be returned the chosen photo. The result is
returned in an Intent object that’s passed to the calling activity’s onActivityResult()
method.
A service is started (or new instructions are given to an ongoing
service) by passing an Intent object to Context.startService()
. Android calls the
service’s onStart()
method and passes it the Intent object.Similarly, an intent can be
passed to Context.bindService()
to establish an ongoing connection between the calling component and a
target service. The service receives the Intent object in an onBind()
call. (If the service
is not already running, bindService()
can optionally start it.) For example, an activity might establish a
connection with the music playback service mentioned earlier so that it
can provide the user with the means (a user interface) for controlling
the playback. The activity would call bindService()
to set up that connection, and then call
methods defined by the service to affect the playback.
A later section, Remote procedure calls, has more details about
binding to a service.
An application can initiate a broadcast by passing an
Intent object to methods like Context.sendBroadcast()
,
Context.sendOrderedBroadcast()
,
and Context.sendStickyBroadcast()
in any of their variations. Android delivers the intent to all
interested broadcast receivers by calling their onReceive()
methods.
For more on intent messages, see the separate article, Intents and
Intent Filters.
,Services
,Broadcast receivers
,Content providers
都属于程序组件
Application Components。
而intents则属于激活组件Activating components。
Content
providers在接受到来自ContentResolver的请求时被激活,而其他三个组件则是被异步消息intents激活。Intent是一个持
有消息内容的Intent对象。作用于activities 和
services时,intent命名被请求的动作,指定数据的URI以便操作。例如传达一个请求给activity来呈现一个图片或者编辑文字。作用于
broadcast receivers时,它命名被广播的动作,例如告诉相关的程序照相键被按了。
针对不同的组件有不同的方法来激活:
activity通过传递Intent对象到Context.startActivity()或者
Activity.startActivityForResult()来运行或者执行一些新内容。如果要看正在反应中的activity是由什么
intent引起的,可以调用它的getIntent()。android系统中调用activity的onNewIntent()来传递给之后的
intents。
一个activity经常会导致另一个的运行。如果希望被其运行的activity返回一个结果,那么就必须调用startActivityForResult()而不是
startActivity()。
service的运行或者接受新指令是通过传递intent给
Context.startService()。android调用service的onStart()并传递一个intent对象给它。
类似的,intent可以被传递给
Context.bindService()来确立调用中的组件和目标service之间的联系。service通过onBind()方法中接受
intent对象。如果service没有启动,bindService()可以选择启动它。
程序可以通过传递Intent对象给例如
Context.sendBroadcast()
, Context.sendOrderedBroadcast()
,
Context.sendStickyBroadcast()来初
始化一个broadcast。android会把intent传递给所有相关的broadcast
receivers,通过调用它们的onReceive()。
原文
Activating components: intents
Content providers are activated when they’re targeted by a requestfrom a ContentResolver. The other three components — activities,
services, and broadcast receivers — are activated by asynchronous
messages called intents
. An intent is an Intent
object that holds the
content of the message. For activities and services, it names the
action being requested and specifies the URI of the data to act on,
among other things. For example, it might convey a request for an
activity to present an image to the user or let the user edit some text.
For broadcast receivers, the Intent object names the action being
announced. For example, it might announce to interested parties that the
camera button has been pressed.
There are separate methods for activating each type of component:
An activity is launched (or given something new to do) by passing
an Intent object to Context.startActivity()
or Activity.startActivityForResult()
.
The responding activity can look at the initial intent that caused it
to be launched by calling its getIntent()
method. Android calls the activity’s onNewIntent()
method to pass it any subsequent
intents.One activity often starts the next one. If it expects a result
back from the activity it’s starting, it calls startActivityForResult()
instead of startActivity()
.
For example, if it starts an activity that lets the user pick a photo,
it might expect to be returned the chosen photo. The result is
returned in an Intent object that’s passed to the calling activity’s onActivityResult()
method.
A service is started (or new instructions are given to an ongoing
service) by passing an Intent object to Context.startService()
. Android calls the
service’s onStart()
method and passes it the Intent object.Similarly, an intent can be
passed to Context.bindService()
to establish an ongoing connection between the calling component and a
target service. The service receives the Intent object in an onBind()
call. (If the service
is not already running, bindService()
can optionally start it.) For example, an activity might establish a
connection with the music playback service mentioned earlier so that it
can provide the user with the means (a user interface) for controlling
the playback. The activity would call bindService()
to set up that connection, and then call
methods defined by the service to affect the playback.
A later section, Remote procedure calls, has more details about
binding to a service.
An application can initiate a broadcast by passing an
Intent object to methods like Context.sendBroadcast()
,
Context.sendOrderedBroadcast()
,
and Context.sendStickyBroadcast()
in any of their variations. Android delivers the intent to all
interested broadcast receivers by calling their onReceive()
methods.
For more on intent messages, see the separate article, Intents and
Intent Filters.
相关文章推荐
- Android 开发文档 程序基础 ——activity组件
- Android 开发文档 程序基础——Activities and Tasks
- Android 开发文档 程序基础 ——Services组件
- Android 开发文档 程序基础 ——Broadcast receivers组件
- Android 开发文档 程序基础——Affinities and new tasks
- Android 开发文档 程序基础 ——Content providers组件
- Android 开发文档 程序基础——Shutting down components
- Android 开发文档 程序基础——The manifest file
- Android 开发文档 程序基础——Intent filters
- Android 开发文档 之程序基础Application Fundamentals
- Android开发之道(4)程序框架基础
- Android程序开发基础之——页面传值
- Android核心基础-3. 开发一个Android程序-3.3. 程序启动过程
- Android开发基础1-概述和SDK安装、文档
- Android程序开发0基础教程(一)
- Android开发之道(4)程序框架基础
- Android开发的基础常识二之程序解读
- Android开发基础——真机测试错误,ADB启动不了,程序安装不了,the connection to adb is down,INSTALL_CANCELED_BY_USER...
- 20155305 2016-2017-2 《Java程序设计》实验四 Android程序开发基础
- Android核心基础-3. 开发一个Android程序-3.2. 安装、卸载程序