Application Fundamentals(3)

Activating Components

Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent.
四种组件类型中的三种—activities, services, and broadcast receivers 会被一种叫做intent的异步消息激活。

Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your app or another.
Intents把运行时相互独立的组件绑定在了一起(你可以把它们看作是请求其他组件的动作的信使),无论这些组件是属于你的应用还是其他应用。

An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.
一个意图是从一个Intent对象创建的,它定义了一个消息来启动任意一个指定的组件或者一类组件,一个意图可以是显式或者隐式的。

For activities and services, an intent defines the action to perform (for example, to “view” or “send” something) and may specify the URI of the data to act on (among other things that the component being started might need to know).
对于activities 和services来说,一个intent定义了要执行的动作(例如,“查看”或者“发送”一些什么)
,也可能指定了要采集的数据的url(在其他组件开始的时候可能需要知道)

For example, an intent might convey a request for an activity to show an image or to open a web page. 例如,一个intent可能会为activity传达一个显示一张图片或者打开一个网页的请求。

In some cases, you can start an activity to receive a result, in which case, the activity also returns the result in an Intent (for example, you can issue an intent to let the user pick a personal contact and have it returned to you—the return intent includes a URI pointing to the chosen contact).
某些情况下。你可以启动一个activity来接收结果,这种情况下,这个activity也会在intent中返回结果(比如,你可以发出一个intent来让用户选择一个联系人,然后让它返回给你,返回来的intent中就包含了一个指向被选联系人的url)

For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates “battery is low”).
对于broadcast receivers来说,intent定义了播出来的通知,(比如,一种就显示设备电池电量不足的广播,只包含一个已知的动作字符串那就是 “battery is low”)

The other component type, content provider, is not activated by intents.
其他的组件类型—content provider,并不是通过intents来激活的。

Rather, it is activated when targeted by a request from a ContentResolver.
取而代之的,当目标请求来自于ContentResolver,它就被激活。

The content resolver handles all direct transactions with the content provider so that the component that’s transactions with the provider doesn’t need to and instead calls methods on the ContentResolver object.
content resolver 直接掌控所有跟content provider的处理,

This leaves a layer of abstraction between the content provider and the component requesting information (for security).
这使得content provider和组件请求信息之间有抽象层(为了安全)

There are separate methods for activating each type of component:
有不同的方法来激活每种组件:

You can start an activity (or give it something new to do) by passing an Intent to startActivity() or startActivityForResult()(when you want the activity to return a result).
你可以通过Intent 去startActivity() 或者 startActivityForResult()(当你想要那个activity返回一个结果的时候)来启动一个activity

You can start a service (or give new instructions to an ongoing service) by passing an Intent to startService(). Or you can bind to the service by passing an Intent to bindService().
你可以通过传输一个Intent 到像startService(). 来启动一个service(或者给一个正在运行的服务新的指令)。你也可以通过Intent去bindService().来绑定一个
service。

You can initiate a broadcast by passing an Intent to methods like sendBroadcast(),sendOrderedBroadcast(), or sendStickyBroadcast().
你可以通过传输一个Intent给类似于sendBroadcast(),sendOrderedBroadcast(), or sendStickyBroadcast().这样的方法来初始化一个broadcast。

You can perform a query to a content provider by calling query() on a ContentResolver.
你也可以通过向ContentResolver调用query() 来对content provider执行查询。

For more information about using intents, see the Intents and Intent Filters document. More information about activating specific components is also provided in the following documents: Activities,Services, BroadcastReceiver and Content Providers.

猜你喜欢

转载自blog.csdn.net/liu2614332/article/details/79987957
今日推荐