Android 开发文档 程序基础——Activating components: intents

来源:互联网 发布:广西财经学院网络 编辑:程序博客网 时间:2024/06/10 04:36

前面说的ActivitiesServicesBroadcast receiversContent 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 request 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.

原创粉丝点击