The Android SDK tools compile the code—along with any data and resource files—into an Android package, an archive file with an .apk
suffix.
Android SDK工具编译代码---及与代码一起的任意数据,资源文件---到一个包中,一个.apk后缀的归档文件.
All the code in a single .apk
file is considered to be one application and is the file that Android-powered devices use to install the application.
所有代码都在单个.apk文件中,当成一个应用,这个文件就是通常安装在Android设备中的应用
Once installed on a device, each Android application lives in its own security sandbox:一旦安装到了一个设备,每个应用生存在它自己的安全沙箱中
In this way, the Android system implements the principle of least privilege. 通过这种方法,Android系统实现了最小特权原则
That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.
默认,每个应用仅仅访问需要工作的组件,并不多做其他的事。这样创建了一个非常安全的环境,应用不能访问系统没有授权的其他部分
However, there are ways for an application to share data with other applications and for an application to access system services:
然而,应用可以有多种方法来与其他应用,共享数据及访问系统服务
That covers the basics regarding how an Android application exists within the system. 上述了一个应用怎样存在于一个系统中的相关基本概念,
The rest of this document introduces you to:这个文档的其他部分将向你介绍
Application components are the essential building blocks of an Android application. 应用组件是构建Android应块的关键。
Each component is a different point through which the system can enter your application.每个组件是一个不同的入点,系统可以从这些点进入到你的应用。
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 application's overall behavior.但是每一个存在的组件都有它自己的一个入点,并扮演一个特定的角色--每一个都是独一无二的构建块,帮助你定义你的应用的整体行为
There are four different types of application 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 application components:下面是四种应用组件
An activity is implemented as a subclass of Activity
and you can learn more about it in the Activities developer guide.一个activity被当作Activity
的子类来实现的,在Activities开发指南中,你可以学到更多关于它的使用
A service is implemented as a subclass of Service
and you can learn more about it in the Services developer guide.一个服务是作为Service
子类来实现的,在Services开发南中你能学到更多关于它的使用
ContactsContract.Data
) to read and write information about a particular person. 因此,任何捅用适当权限的应用,可以查询内容提供者的部分来(比如
ContactsContract.Data
)读取和写入关于某个人的信息
A content provider is implemented as a subclass of ContentProvider
and must implement a standard set of APIs that enable other applications to perform transactions.
一个内容提供者被当作的子类实现,并且必须实现一套标准的APIs,以让其他的应用能执行交换操作。
For more information, see the Content Providers developer guide.参考Content Providers开发指南,以了解更多信息.
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.
一个广播接收者,是当作BroadcastReceiver
子类被实现的.每个广播接收者都是从Intent
对象衍生出来的。更多信息,请参考BroadcastReceiver
类
A unique aspect of the Android system design is that any application can start another application’s component.
任何一个应用能启动另一个其他应用的组件,是Android系统设计独一无二的方面(aspect).
For example, if you want the user to capture a photo with the device camera, 比如,你想要用设备的照相机拍一张图片
there's probably another application that does that and your application 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 application. Instead, you can simply start the activity in the camera application that captures a photo.
你并不需要合并(包含)或者甚至是链接camera应中的代码; 而只是,简单的启动camera应用中的活动,来拍照就可以了.
When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application
当拍照完成,甚至把照片返回给你的应用,所以你能使用它。对于用户来讲,camera像是你应用中一部分
When the system starts a component, it starts the process for that application (if it's not already running) and instantiates the classes needed for the component.
当系统开启一个组件时,它会启动那个应用的进程(如果该应用没有运行),并实例化该组件所需要的类.
For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process.
举例,如果你的应用开启一个camera应用的activity,来拍照,这个activity将运行在属于camera应用的进程中,而不是在你的应用的进程中
Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main()
function, for example).
因此,不像大多数其他的系统的应用,Android应用,没有单个的入点(比如没有main()函数)
Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component from another application.
因为系统运行的每个应用,在一个带有文件权限的,独立的进程中,这样限制了对其他应用的访问,你的应用不能直接访问其他应用中的组件
The Android system, however, can. So, to activate a component in another application, 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系统也能激活其他应用的组件.你必须传一个消息给系统,指定你想要启动的组件,然后系统为你激活这个组件
Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent.
4个组件中的其中三个组件---activities,serivces,和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 application 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是一个由Intent创建的对象.该对象定义了一个激活某个特定组件或者某个组件类型的消息,一个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(意图)定义了一个要执行的动作(比如:to”view”或"send" 些什么),并指定了要采用的URI格式的数据(其中一些,是其他组件启动所需要知道的)
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中会包含一个指向选定联系方式的URI.)
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").对于广播接收者,intent只是定义了一个做为广播的公告.(比如,一个广播指出,设备电池低,它只是包含了一个动作字串,表示”电池低”)
The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver
.
其他组件,内容提供者,不会被intents所激活.进一步讲,它是内容解释者(ContentResolver
)所请求的目标所激活的
The content resolver handles all direct transactions with the content provider 内容解释者,处理所有与内容提供者的直接交换
so that the component that's performing transactions with the provider doesn't need to and instead calls methods on the ContentResolver
object. 所以组件不需要执行与提供者交换,而是调用ContentResolver
对象方法.(这一句不好理解。)
This leaves a layer of abstraction between the content provider and the component requesting information (for security).为了安全起见,组件请求信息与内容提供者之间有一个抽象层.
There are separate methods for activiting each type of component:下面是激活各种类型组件的几个方法
Intent
to startActivity()
or startActivityForResult()
(when you want the activity to return a result). 你可以通过传一个(或者一些要做新的事情)Intent
参数给startActivity()
或startActivityForResult()
(当你想要activity返回一个参数)函数(),来启动一个activity.Intent
to startService()
. Or you can bind to the service by passing an Intent
to bindService()
. 你可以传一个Intent
给startService()
方法,(或给一个新的指令给正在运行的服启),或者你可以传一个Intent
给bindService()
方法来邦定到服务.Intent
to methods like sendBroadcast()
, sendOrderedBroadcast()
, or sendStickyBroadcast()
.query()
on a ContentResolver
. 你可以对ContentResolver
调用query()
方法,对内容提供者进行查询For more information about using intents, see the Intents and Intent Filters document.关于使用intents的详细信息,请看ntents and Intent Filters 文档
More information about activating specific components is also provided in the following documents: Activities, Services, BroadcastReceiver
and Content Providers.
在后面的文档中,也有一些关于激活某个组件的信息Activities, Services, BroadcastReceiver
and Content Providers.
Before the Android system can start an application component, the system must know that the component exists by reading the application's AndroidManifest.xml
file (the "manifest" file). 在Android系统开启一个应用组件之前,系统必须通过读取AndroidManifest.xml文件来知道组件的存在
Your application must declare all its components in this file, which must be at the root of the application project directory.
你的应用必须把它所有的组件声明在这个文件中,并且必须在应用工程的根目录下
The manifest does a number of things in addition to declaring the application's components, such as:
这个manifest文件除了声明组件外,还处理了许多其他的事情,比如
The primary task of the manifest is to inform the system about the application's components. For example, a manifest file can declare an activity as follows:
manifest文件的主要任务是告诉系统,应用的组件,比如,一个manifest可以这样声明一个activity
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:icon="@drawable/app_icon.png" ... >
<activity android:name="com.example.project.ExampleActivity"
android:label="@string/example_label" ... >
</activity>
...
</application>
</manifest>
In the <application>
element, the android:icon
attribute points to resources for an icon that identifies the application.在<application>
元素中,android:icon
指定应用的icon资源
In the <activity>
element, the android:name
attribute specifies the fully qualified class name of the Activity
subclass and the android:label
attributes specifies a string to use as the user-visible label for the activity.
在<activity>
元素中的,android:name
属性,指定Activity
子类的完全类名
You must declare all application components this way:你必须这样声明 所有应用的组件
<activity>
elements for activities 声明活动的元素<service>
elements for services 声明服务的元素<receiver>
elements for broadcast receivers 声明广播接收者元素<provider>
elements for content providers 声明内容提供者元素Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run.
在你代码中包含的,Activites,services和内容提供者,若没有在manifest中声明,对系统来说是不可见的,即将永远不会运行
However, broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver
objects) and registered with the system by calling registerReceiver()
.但是,广播接收者即可以在manifest中声明,也可以在代码中动态创建(做为BroadcastReceiver
对象)并且通过registerReceiver()
方法向系统注册
For more about how to structure the manifest file for your application, see the The AndroidManifest.xml File documentation. 了解manifest文件的详细构建过程,请看The AndroidManifest.xml File文档
As discussed above, in Activating Components, you can use an Intent
to start activities, services, and broadcast receivers.
就如在上面的Activating Components中所讨论的,你可以用一个Activating Components启动activities,services和broadcast 接收者.
You can do so by explicitly naming the target component (using the component class name) in the intent. 你也可以在intent中显式的指定目标组件(使用组件类名)
However, the real power of intents lies in the concept of intent actions. 然而,intent真正强大的是它的intent action.(动作)
With intent actions, you simply describe the type of action you want to perform (and optionally, the data upon which you’d like to perform the action) and allow the system to find a component on the device that can perform the action and start it.
通过使用intent动作,你只须简单的描述你要执行的action类型,(并且,可选的与执行动作有关的数据),并且允许系统在设备上找到一个组件,这样就可以执行那个动作并启动它
If there are multiple components that can perform the action described by the intent, then the user selects which one to use.
如果有多个组件可以执行,intent指定的action,那么用户选择执行那一个.
The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other applications on the device.
通过比较设备上的其他应用的manifest文件上的intent filters与接收到的intent.系统确定那个组件可以响应一个intent.
When you declare a component in your application's manifest, you can optionally include intent filters that declare the capabilities of the component so it can respond to intents from other applications. 当你在你的应用的manifest中声明一个组件时,你可以可选择包括intent filters(意图过滤器),来指定组件的功能,以让其能响应其他应用的intents.
You can declare an intent filter for your component by adding an <intent-filter>
element as a child of the component's declaration element.
你可以加一个组件声明的元素的子元素<intent-filter>
,为你组件声明一个意图过滤器,
For example, an email application with an activity for composing a new email might declare an intent filter in its manifest entry to respond to "send" intents (in order to send email).
比如,一个email应用中,新建emali的一个activity可能在它的manifest 中声明了一个意图过滤器,以便能响应”send”意图(为了发送邮件)
An activity in your application can then create an intent with the “send” action (ACTION_SEND
), which the system matches to the email application’s “send” activity and launches it when you invoke the intent with startActivity()
.然后,在你的应用中的一个activity,创建了一个带有”send” ACTION_SEND
的意图,.当你调用startActivity()
方法,启动该意图过滤器时,系统将其匹配到email应用的“send”活动,并运行它。
For more about creating intent filters, see the Intents and Intent Filters document. 关于创建意图过滤器的详细信息,参考Intents and Intent Filters 文档
There are a variety of devices powered by Android and not all of them provide the same features and capabilities. 有许多设备装了Android,但它们并不提供所有相同的特点和功能.
In order to prevent your application from being installed on devices that lack features needed by your application, 为了避免你的应用,装在一个没有你应用所必特征的设备上.
it's important that you clearly define a profile for the types of devices your application supports by declaring device and software requirements in your manifest file.
通过在你的manifest文件中声明软件硬件要求,明了的指出你的应用支持的硬件类型是非常重要的
Most of these declarations are informational only and the system does not read them, but external services such as Android Market do read them in order to provide filtering for users when they search for applications from their device.大多数声明仅仅只是信息,系统并不读取他们,但像Android市场这样的其他服务,将读取它们,以便让用户在为他们的设备寻找应用时,可以进行筛选.
For example, if your application requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file.
比如,如果你的应用需要有照相机,并且使用的API是2.1(API Level 7),你应在你r的manifest文件中声明这些要求.
That way, devices that do not have a camera and have an Android version lower than 2.1 cannot install your application from Android Market.
这样,那些没有照相机并且Android版本低于2.1的设备,就不能从Android市场上安装你的应用.
However, you can also declare that your applicaiton uses the camera, but does not require it. 但,你也可以声明你的应使用camera,但不必须要求
In that case, your application must perform a check at runtime to determine if the device has a camera and disable any features that use the camera if one is not available.
那种情况,你的应用必在运行时一个检查,以确定设备是否有一个照相机,如果没有照相机,并禁止与照相相关的功能。
Here are some of the important device characteristics that you should consider as you design and develop your application: 下面是一些重要的设备特性,你在设计和开发应用时必须要考虑的..
The screen sizes are: small, normal, large, and extra large.屏幕大小:小,正常,大和极大
The screen densities are: low density, medium density, high density, and extra high density. 屏幕解释度:低解释度,中解释度,高解释度,和极高解释度
By default, your application is compatible with all screen sizes and densities, because the Android system makes the appropriate adjustments to your UI layout and image resources. 黑认情况下,你的应用是兼容所有屏幕尺寸和解释度的,因为Android系统对此做了适当的调整,以使得它适合你的UI布局和图像资源
However, you should create specialized layouts for certain screen sizes and provide specialized images for certain densities, using alternative layout resources, and by declaring in your manifest exactly which screen sizes your application supports with the <supports-screens>
element.
然而,你应为某个屏幕尺寸创建特殊的布局,并为某些解释度提供特定的图像,使用可选的资源,并在你的manifest文件中用<supports-screens>
元素声明,以明确指出你的应用支持的屏幕尺寸.
For more information, see the Supporting Multiple Screens document. 更多信息,参考Supporting Multiple Screens文档
<uses-configuration>
element.如果你的应用必须要一个特别的输入硬件,那么你应在你的应用中使用
<uses-configuration>
元素声明However, it is rare that an application should require a certain input configuration. 但时,应用必须要一个特别的输入配置的情况是极少的.
<uses-feature>
element.
<uses-feature>
元素声明你的应用支持的特征.
<uses-sdk>
element. 如果你使用的APIs是在1.0版之后,加入到平台的,你应该用
<uses-sdk>
元素,声明最小API级别,这样就指出了那些API将被采用.
For more information about how Android Market filters applications based on these (and other) requirements, see the Market Filters document.
更多关于Android市场如何基于这些需求过滤的,请看Market Filters文档
An Android application is composed of more than just code—it requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the application. 一个Android应用的组成不仅只是代码----它还有与代码独立的资源,比如图像,音频文件,及与应用可显图像任何其他相关的(东东)
For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files.
比如,你应该定义动画,菜单,风格,颜色,和用XML文件定义活动的布局
Using application resources makes it easy to update various characteristics of your application without modifying code and—by providing sets of alternative resources—enables you to optimize your application for a variety of device configurations (such as different languages and screen sizes). 使用应用资源,能让你的应用在不修改任何代码的情况下容易的升级各种特性---并且通过提供一套可选取的资源--能优化你的应用在各种配置不同的设备中的表现(比如不同的语言和屏幕尺寸)
For every resource that you include in your Android project, the SDK build tools define a unique integer ID, which you can use to reference the resource from your application code or from other resources defined in XML.
对于每个包含在你的Android工程中的资源,SDK将其定义成一个唯一的整型ID,这样你就可以在你的代码中或在XML文件中定义的其他资源中引用它For example,
if your application contains an image file named logo.png
(saved in the res/drawable/
directory), the SDK tools generate a resource ID named R.drawable.logo
, which you can use to reference the image and insert it in your user interface. 如果你的应用包括一个图片名字是logo.png(保存在res/drawable/directory),SDK工具将生成一个资源ID命名成R.drawable.logo,你可以用它来引用图片,并插入你的用户界面中
One of the most important aspects of providing resources separate from your source code is the ability for you to provide alternative resources for different device configurations. 提供与你的代码分开的资源的一个很重要的方面是,使得你能为不同的配置的设备提供可选资源.
For example, by defining UI strings in XML, you can translate the strings into other languages and save those strings in separate files.
比如,在XML中定义UI字串,你可以把字串翻译成各种不同的语言并保存在不同的文件中
Then, based on a language qualifier that you append to the resource directory's name (such as res/values-fr/
for French string values) and the user's language setting, the Android system applies the appropriate language strings to your UI. 然后,以基于语言限定词,你可以追加资源目录名(比如res/values-fr/ 用语法语资源),和用户语言设置,Android系会将相应的资源应用到你的UI中.
Android supports many different qualifiers for your alternative resources. Android为你的可选资源,支持许多不同的qualifiers (限定词)
The qualifier is a short string that you include in the name of your resource directories in order to define the device configuration for which those resources should be used.
限定词是一个包括在你的目录名中的一个简短的字串,是为了定义那些资源将用在,该配置的设备上.
As another example, you should often create different layouts for your activities, depending on the device's screen orientation and size. 再如,由于设备的屏幕的方向和尺寸不同,你通常需要为你的活动定义不同的布局.
For example, when the device screen is in portrait orientation (tall), you might want a layout with buttons to be vertical, but when the screen is in landscape orientation (wide), the buttons should be aligned horizontally. 比如,若设备的屏幕是竖向(高),你可能要一个带有重直button 的布局,当屏幕是横向的(宽),按钮应是水平对齐的
To change the layout depending on the orientation, you can define two different layouts and apply the appropriate qualifier to each layout's directory name.
要根据方向来改变布局,你要定义两个不同的布局,并在布局的目录名中使用相应的限定词(qualifier)
Then, the system automatically applies the appropriate layout depending on the current device orientation. 然后,系统将自动根据当前的设备朝向来应用相应的布局
For more about the different kinds of resources you can include in your application and how to create alternative resources for various device configurations, see the Application Resources developer guide. 要详细了解,你的应用中能包含的各种资源,及如何为各种配置的设备创建可选资源,请看Application Resources开发指南
<终于翻译完了,花了一天多的时间>