Application Fundamentals--Activating components: intents(组件的激活)

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.

翻译:ContentResolver实例向内容提供组件发送了一个请求可以激活内容提供组件,其他三类:activitiy组件、服务组件、广播接收组件的激活是借助一个我们称之为 intents的异步消息请求对象,对于activitiy组件、服务组件,这个称之为 intents的异步消息请求对象中包含了请求执行的动作(action)、要操作的数据的URI以及其他必要的信息,比如一个异步消息请求对象可以请求一个 activitiy组件为用户显示一个图像文件或是让用户编辑指定的数据。如果这时候的异步消息请求对象(Intent对象)是要发送给广播接收组件的,那么Intent对象中将包含了要通告的内容,比如它通告发生了相机按钮被压下的动作给关注该动作的对象。

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.【翻译:activity的激活是通过传递一个Intent对象给Context.startActivity()方法 或 Activity.startActivityForResult()方法. 之后,被激活的activity可以执行自己的 getIntent() 方法来得到激活它的那个intent对象. 这是因为 Android 已经调用过被激活的activity的 onNewIntent() 方法把激活该activity的intent对象传递给了被激活的activity.】

      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.【翻译:一个activity常常会去激活下一个activity的,如果说前一个activity希望从被激活的activity中得到返回值的话,它应该调用 startActivityForResult() 方法而不是调用 startActivity()方法。例如如果激活一个activity, 让用户挑选一个图像文件,事实上第一个activity是需要得到用户选定的图像文件信息的,这些返回信息是直接存放在 Intent 对象,然后直接可以带回到调用者 activity的 onActivityResult() 方法中的(方法参数).】
   
      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.【翻译:service组件的启动或运行中的服务组件接收到新指令操作都同样是借助intent对象的,Context.startService()方法被执行后,Android 将立刻调用该服务组件的onStart() 方法来响应,同时 Intent 对象也传递给了这个方法.】

      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.

      翻译:类似, 一个 intent 请求对象也可以作为 Context.bindService() 方法的参数,该方法被执行后,调用方组件和目标服务之间建立了一个即时连接, android系统调用该服务组件的 onBind() 方法的时候同时把intent对象传递给了这个方法(参数). (如果服务没有启动,那么Android系统可以调用 bindService() 来启动该服务.) 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.


Shutting down components--组件关闭

A content provider is active only while it's responding to a request from a ContentResolver. And a broadcast receiver is active only while it's responding to a broadcast message. So there's no need to explicitly shut down these components.【翻译:内容提供组件只是在被ContentResolver请求后做应答的时候处于激活状态,广播接收组件只是在对广播消息做应答时候处于被激活状态,所以这两个组件不需要显式关闭。】

Activities 和 services 组件有其特殊性,都有需要长时间处于激活状态,所以Android 提供了具体方法来关闭它们:

    * activity 调用它自身的 finish() 方法就可以关闭自己. 一个 activity 也可以通过调用finishActivity()来关闭另外一个activity (必须是由它执行startActivityForResult()) 方法所激活的activity).
    * service可以调用自身的 stopSelf() 方法或是调用 Context.stopService()方法关闭服务.

Components might also be shut down by the system when they are no longer being used or when Android must reclaim memory for more active components. A later section, Component Lifecycles, discusses this possibility and its ramifications in more detail.

你可能感兴趣的:(android,UP)