Application Fundamentals(2)

App Components

App components are the essential building blocks of an Android app. Each component is a different point through which the system can enter your app. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your app’s overall behavior.
应用组件是安桌应用最基础的组成模块。每个组件都是一个系统进入应用的不同的点。对于用户来说,并不是所有的组件都是一个入口点,有些是相互依赖的。但是每一个都存在于他们自己的实体中并且扮演一个特定的角色—每一个都是独特的构建模块,帮助你定义应用的所有行为。

There are four different types of app components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.
有四种类型的应用组件。每种服务都有不同的目的和不同的生命周期,生命周期定义了组件是怎么被创建和销毁的。

Here are the four types of app components:
下面是四种应用组件:

Activities

An activity represents a single screen with a user interface. For example, an email app might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails.
一个activity就代表了一个带有用户界面的单一屏幕。比如,一个邮箱应用可能就有一个用于显示新邮件列表的activity,一个用于撰写邮件的activity,还有一个就是用于阅读邮件的activity

Although the activities work together to form a cohesive user experience in the email app, each one is independent of the others.
尽管这些邮件应用的activities一起工作组成了一种粘性的用户体验,但是他们每一个都是独立的。

As such, a different app can start any one of these activities (if the email app allows it). For example, a camera app can start the activity in the email app that composes new mail, in order for the user to share a picture.
同样的,不同的应用可以启动其他应用的任何一个activity(只要其他应用允许)。比如,在邮箱应用中可以启动一个照相机应用的activity用于组成新邮件,以便于向用户分享一张照片。

An activity is implemented as a subclass of Activity and you can learn more about it in the Activities developer guide.
一个activity是一个实现为Activity类的子类,你可以在 the Activities developer guide. 章节学习到更多关于它的知识。

Services

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes.
Services是一个运行于后台,用于执行耗时操作或者为远程进程执行操作的组件。

A service does not provide a user interface. For example, a service might play music in the background while the user is in a different app, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.
service不提供用户界面。例如,当用户在另外一个应用的时候,service可能在后台播放音乐,也或者从网络上获取数据,而不会阻塞用户与activity的交互。

A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.
一个service是一个实现为Service类的子类,你可以在 the Service developer guide. 章节学习到更多关于它的知识。

Content providers

A content provider manages a shared set of app data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your app can access.
content provider管理一组共享的应用数据。你可以把数据保存在文件系统,数据库,网络,或者其他应用可以访问的永久存储的位置

Through the content provider, other apps can query or even modify the data (if the content provider allows it).
通过content provider,别的应用可以查询甚至更改数据( 如果 content provider 允许的话 )

For example, the Android system provides a content provider that manages the user’s contact information. As such, any app with the proper permissions can query part of the content provider (such as Contacts Contract.Data) to read and write information about a particular person.
例如,安卓系统提供了管理用户联系人信息的
content provider。这样的话,任何拥有相应权限的应用都可以查询content provider的部分内容(比如联系人数据)以读写特定人的信息。

Content providers are also useful for reading and writing data that is private to your app and not shared. For example, the Note Pad sample app uses a content provider to save notes.
Content providers 也用于读写对于你的应用来说是私有和不共享的数据。比如记事本示例程序使用content provider来保存笔记。

A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other apps to perform transactions. For more information, see the Content Providers developer guide.
一个 content provider 是一个实现为content provider 类的子类,你可以在 the content provider developer guide. 章节学习到更多关于它的知识。

Broadcast receivers

A broadcast receiver is a component that responds to system-wide broadcast announcements.
broadcast receiver 是一种对于整个系统的广播通知进行响应的组件。

Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured.
许多广播发源自系统,例如,一种广播通告屏幕关了,电池电量过低,或者拍摄了一张照片。

Apps can also initiate broadcasts—for example, to let other apps know that some data has been downloaded to the device and is available for them to use.
应用也可以发起广播,比如,让其他应用知道有些数据已经下载到了设备中,并且可以拿来使用。

Although broadcast receivers don’t display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs.
尽管broadcast receivers 不显示用户界面,但是当一个广播事件发生的时候可能会创建一个状态栏以提醒用户。

More commonly, though, a broadcast receiver is just a “gateway” to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.
多数情况下,broadcast receiver对于其他组件来说只是一个“网关”,旨在做非常少量的工作。例如,基于这事件创建了一个执行某些操作的service

A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the Broadcast Receiver class.
一个 broadcast receiver 是一个实现为broadcast receiver 类的子类,每个广播都是基于一种意图来传递的。你可以通过查看 Broadcast Receiver 类来学习到更多关于它的知识。

A unique aspect of the Android system design is that any app can start another app’s component.
安卓系统一个独特的方面就是任何应用都可以启动另一个应用的组件。

For example, if you want the user to capture a photo with the device camera, there’s probably another app that does that and your app can use it, instead of developing an activity to capture a photo yourself.
比如,如果你希望使用设备的摄像头来拍一张照片,可能另一个应用就可以做到而你只需要直接使用它,而无需通过开发一个activity来自己拍照。

You don’t need to incorporate or even link to the code from the camera app. Instead, you can simply start the activity in the camera app that captures a photo. When complete, the photo is even returned to your app so you can use it. To the user, it seems as if the camera is actually a part of your app.
你不需要合并甚至链接相机的代码。取而代之的,你可以简单的启动相机中的activity来进行拍照。拍照完成之后,照片就会返回给你的应用你就可以使用它了。对于用户来说,看起来相机就是你应用的一部分。

When the system starts a component, it starts the process for that app (if it’s not already running) and instantiates the classes needed for the component.
当系统启动一个组件的时候,它会为应用启动进程(如果它还没有启动过)并且实例化那些组件需要的类。

For example, if your app starts the activity in the camera app that captures a photo, that activity runs in the process that belongs to the camera app, not in your app’s process.
比如,如果你的应用启动了照相机应用中的activity来进行拍照,那么activity运行所在的进程是属于照相机应用的,而不是你的应用。

Therefore, unlike apps on most other systems, Android apps don’t have a single entry point (there’s no main() function, for example).
因此,不像在大多数别的系统上的应用,安卓应用没有单一的入口点(比如没有main函数)

Because the system runs each app in a separate process with file permissions that restrict access to other apps, your app cannot directly activate a component from another app.
因为系统让每一个应用运行在独立的进程中,并且使用文件权限来限制访问其他应用,所以你的应用不能直接启动别的应用的组件。

The Android system, however, can. So, to activate a component in another app, 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.
然而安卓系统却可以这样。如果要启动别的应用中的组件,你必须发送一条信息给系统以指定你希望启动的特有的组件的意图。然后系统就会为你启动那个组件。

猜你喜欢

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