当前位置: 代码迷 >> Android >> Android四大组件的了解
  详细解决方案

Android四大组件的了解

热度:57   发布时间:2016-04-28 00:43:13.0
Android四大组件的理解

?分享一下,今天在Android开发文档-开发者指南中看到的: ? ? ? ??

? ? ? ?

? ? ? ? ? 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.

? ? ? ? ?应用程序组件是一个Android应用程序的基本构造块。每个组件都是一个不同的角度,通过该系统,可以进入你的应用程序。不是所有的组件都是为用户和一些互相依赖的实际的切入点,但每一个都作为一个独立的的实体,扮演着特定的角色——每一个都是一个可以帮助确定您的应用程序的整体行为独特的构建快。

?

? ? ?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:

? ? ?以下是四种类型的应用程序组件:

Activity:

? ? ?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. Although the activities work together to form a cohesive user experience in the email app, each one is independent of the others. 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表示为单个屏幕的用户界面。比如,一个email应用程序可能用一个Activity显示新的email列表,用另一个Activity去写Activity,并且使用另外的Activity来阅读Email。虽然活动一起形成一个有凝聚力的用户体验的电子邮件应用程序,但是每一个都是相互独立的。同样的,不同的应用程序可以启动这些Activity中的任何一个(如果Email程序允许他这么做)。比如所,一个相机应用为了分享一张照片可以启动一个Email应用中的Activity来创建一个新的Email。

?

? ? ? An activity is implemented as a subclass of Activity and you can learn more about it in the Activitiesdeveloper guide.

? ? ?你的应用中的Activity应该是继承了Activity的一个子类,如果想要了解更多,可以参照Activities开发指南。

?

Service:

? ?A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. 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是一个运行在后台的、执行耗时操作或者执行跨进程操作的组件。服务没有用户界面。比如所,当用户在其他应用程序的时候,一个服务可能在后台播放音乐或者它可能从网络上读取数据但并不会阻塞用户和Activity交互。另外一个组件,比如一个Activity,为了与Service交互,能够启动服务并且让它运行或者绑定到它。

?

? ? ?A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.

? ? ?你的应用中的Service应该是继承了Service的一个子类,如果你想了解更多,可以参照Services开发指南。

?

Content Provider:

? ? ?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. Through the content provider, other apps can query or even modify the data (if the content provider allows it). 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 ContactsContract.Data) to read and write information about a particular person.

? ? ? 一个content provider管理一个分享数据的集合。你能够存储数据到文件系统、SQLite数据库、在网络上或者任何其它的你能够访问的持久化存储位置。通过Content Provider,其他应用程序能够查询或者修改数据(如果Content Provider允许它这么做)。比如说,Android系统提供了一个管理用户联系人信息的Content Provider。同样的,任何具有适当权限的应用程序能够查询Content Provider(比如ContactsContract.Data) 去读取或者修改关于特定联系人的信息。

?

? ? ?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 Provider也能够在读取或者写入你的应用的没有共享的私有数据。比如Note Pad 例子应用程序使用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 Providersdeveloper guide.

? ? ? 你的应用中的ContentProvider应该是ContentProvider的一个子类,并且为了让其他应用程序能够操作事务,必须实现一个标准的API集合。如果您想要得到更多的信息,可以参照Content Providers开发者指南。

?

Broadcast Receiver:

? ? A broadcast receiver is a component that responds to system-wide broadcast announcements. 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. 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是一个响应系统范围内发布的广播的组件。很多广播是从系统发布出来的——比如,一个广播通知屏幕已经关闭,电池电量低或者相机已经捕获了一张图片。应用程序也能够发出广播——比如,让其他应用程序知道某些数据已经被下载到设备上,可供它们使用。通过broadcast receiver不能够显示一个用户界面,当广播发生的时候,可以通过创建一个状态栏通知提示用户。更常见的是,一个broadcast receiver仅仅是一个为了做一个非常短暂的事情的到其他组件的“网关”。例如,它可能会启动一个服务来执行基于事件的一些工作。

?

? ? ? ?A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class.

? ? ? ?你的应用程序中的一个broadcast receiver是一个继承自BroadcastReceiver的一个子类并且每一个broadcast传第一个Intent 对象。如果您想要获得更多数据,请参考BroadcastReceiver类。

?

? ? ?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. 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.

? ? ? ?Android系统设计的一个特殊的层面是任何应用都能够启动其他应用的组件。例如,如果你想用户使用设备的相机去捕获一张图片,有可能您通过其他应用的去这么做,而不是自己开发一个Activity去这么做。你不需要把相机应用的代码放到你的应用中去,而是改为简单的启动相机应用的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. Therefore, unlike apps on most other systems, Android apps don't have a single entry point (there's no main() function, for example).

? ? ? ? 当系统启动一个组建的时候,它开始一个这个应用的进程(如果他没有正在运行),并且实例化这个组件所需要的类。例如说,如果你的应用启动一个Camera应用的Activity去不活一张图片,该Activity属于相机应用的进程中,而不是在你的用用的进程中。因此,不像其他大多数系统的应用程序,Android应用程序没有一个单独的入口点(例如,没有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.

? ? ?由于每个应用程序运行在独立于其它应用程序文件权限的系统中,你的呢应用程序不能直接从另一个程序激活一个组件。但是Android系统可以,所以,要激活另一个应用程序的组件,则必须将消息传递到你的Intent所指定的特定组件中,然后系统为你激活那个组件。

?

  相关解决方案