【翻译】(2)应用程序基本要素

【翻译】(2)应用程序基本要素

 

see

http://developer.android.com/guide/topics/fundamentals.html

 

原文见

http://developer.android.com/guide/topics/fundamentals.html

 

Application Fundamentals

 

应用程序基本要素

 

----------------

Quickview

 

快速概览

 

* Android applications are composed of one or more application components (activities, services, content providers, and broadcast receivers)

 

* Android应用程序由一个或多个应用程序组件(活动,服务,内容提供者和广播接收器)组成。

 

* Each component performs a different role in the overall application behavior, and each one can be activated individually (even by other applications)

 

* 每个组件在整个应用程序行为中扮演不同角色,每个组件可以被单独激活(甚至是被其它应用程序激活)

 

* The manifest file must declare all components in the application and should also declare all application requirements, such as the minimum version of Android required and any hardware configurations required

 

* 清单文件必须声明应用程序内的所有组件,还应该声明所有应用程序的要求(注:运行条件),诸如所需Android的最小版本和所需的任意硬件配置

 

* Non-code application resources (images, strings, layout files, etc.) should include alternatives for different device configurations (such as different strings for different languages and different layouts for different screen sizes)

 

* 非代码应用程序资源(图片,字符串,布局文件,等等)应该针对不同设备配置包含可选方案(诸如对不同语言有不同字符串,对不同屏幕大小有不同布局)

 

In this document 本文目录

Application Components 应用程序组件

Activating components 激活组件 

The Manifest File 清单文件

Declaring components 声明组件

Declaring application requirements 声明应用程序最低运行条件

Application Resources 应用程序资源

 

----------------

 

Android applications are written in the Java programming language. 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. 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.

 

Android应用程序用Java编程语言书写。Android SDK工具编译代码——包括代码和资源文件——到Android包,一种带.apk后缀的档案文件。单一.apk文件中的所有代码可以认为是一个应用程序和供Android设备使用的安装应用程序的文件。

 

Once installed on a device, each Android application lives in its own security sandbox:

 

一旦安装到设备上,每个应用程序在它自己的安全沙箱内生存:

 

* The Android operating system is a multi-user Linux system in which each application is a different user.

 

* Android操作系统是一个多用户Linux系统,里面的每一个程序是不同的用户。

 

* By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them.

 

* 默认,系统赋予每个应用程序一个唯一的Linux用户ID(ID只被系统使用,对于应用程序是未知的)。系统在一个应用程序中设置所有文件的权限,致使只有赋予该应用程序的用户ID才可以访问它们。

 

* Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications.

 

* 每个进程拥有它自己的虚拟机(VM),所以一个应用程序的代码与其它程序隔离运行。

 

* By default, every application runs in its own Linux process. Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications.

 

* 默认,每个应用程序运行在它自己的Linux进程。当任意应用程序的组件需要被执行时Android启动进程,当它不再需要或当系统必须恢复其它应用程序的内存时关闭进程。

 

In this way, the Android system implements the principle of least privilege. 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.

 

用这种方式,Android系统实现最小特权原则。即,每个应用程序,默认下,仅拥有针对它所需用于完成其工作的组件的访问权,而不会有更多东西。这创造了一个非常安全的环境,在其中应用程序无法访问系统中未被授权的部分。

 

However, there are ways for an application to share data with other applications and for an application to access system services:

 

然而,一个应用程序有办法共享数据给其它应用程序和访问系统服务:

 

* It's possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each other's files. To conserve system resources, applications with the same user ID can also arrange to run in the same Linux process and share the same VM (the applications must also be signed with the same certificate).

 

* 可以安排两个应用程序共享相同的Linux用户ID,这种情况下它们可以访问各自的文件。要保存(注:这里的意思是节约)系统资源,带有相同用户ID的应用程序还可以安排运行在相同的Linux进程以及共享相同的虚拟机(应用程序还必须赋予相同证书)。

 

* An application can request permission to access device data such as the user's contacts, SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. All application permissions must be granted by the user at install time.

 

* 一个应用程序可以请求权限访问设备数据诸如用户电话簿、SMS短信、可挂载存储(SD卡)、摄像头、蓝牙,以及其它。所有应用程序权限必须由用户在安装期授权。

 

That covers the basics regarding how an Android application exists within the system. The rest of this document introduces you to:

 

那些内容涵盖关于一个Android应用程序如何与系统共存的基础。此文档的其余内容向你介绍:

 

* The core framework components that define your application.

 

* 定义你的应用程序的核心框架组件。

 

* The manifest file in which you declare components and required device features for your application.

 

* 清单文件,其中你声明你的应用程序的组件和所需设备特性。

 

* Resources that are separate from the application code and allow your application to gracefully optimize its behavior for a variety of device configurations.

 

* 资源,独立于应用程序代码并且允许你的应用程序针对各种设备配置优雅地优化它的行为。

 

Application Components

 

应用程序组件

 

Application components are the essential building blocks of an Android application. 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.

 

应用程序组件是Android应用程序的本质构建模块。每个组件是不同的点,通过它系统可以进入你的应用程序。不是所有组件是实际的用户入口点,有一些依赖于其它组件,但每个组件都作为它自己的实体而存在,扮演其特定的角色——每个组件是一个唯一的构建模块,有助于定义你的应用程序的整体行为。

 

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:

 

这里介绍四种应用程序组件:

 

Activities

 

活动

 

An activity represents a single screen with a user interface. For example, an email application 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 application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

 

一个活动代表一个带用户界面的单独屏幕。例如,一个电子邮件应用程序可能有一个活动展示新电邮的列表,另一个活动则是用来写邮件,还有另一活动则是用来读邮件。虽然在电子邮件应用程序中活动在一起工作,组合成拼合的用户体验,但每个活动独立于其它。正因如此,不同的应用程序可以启动这些活动中的其中一个(如果电子邮件应用程序允许)。例如,一个摄像头应用程序可以启动电子邮件应用程序中写邮件的活动,以便于用户分享图片。

 

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

 

一个活动实现为Activity的子类,你可以在活动开发者指引中获知更多相关信息。

 

Services

 

服务

 

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 application, 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.

 

服务是运行于在后台执行长期运行操作或执行远程处理工作的组件。服务不提供用户界面。例如,一个服务当用户在不同应用程序时可以在后台播放音乐,或者可以通过网络取得数据,而不阻塞用户界面和活动。另一个组件,诸如一个活动,可以启动服务,然后让它运行或绑定它以便于与其交互。

 

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

 

服务实现为Service的子类,你可以在服务开发者指引中获知更多相关信息。

 

Content providers

 

内容提供者

 

A content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. Through the content provider, other applications 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 application with the proper permissions can query part of the content provider (such as ContactsContract.Data) to read and write information about a particular person.

 

内容提供者管理共享的应用程序数据集合。你可以在文件系统内,SQLite数据库,网络上,或你的应用程序可访问的其它任意可持久存储位置上存储数据。通过内容提供者,其它应用程序可以查询,或者甚至修改数据(如果内容提供者允许)。例如,Android系统提供一个内容提供者管理用户电话簿信息。因此,任何拥有合适权限的应用程序可以查询内容提供者的部分(诸如ContactsContract.Data)以读写特定人的相关信息。

 

Content providers are also useful for reading and writing data that is private to your application and not shared. For example, the Note Pad sample application uses a content provider to save notes.

 

内容提供者还可能有助于读写对于你的应用程序是私有的、不共享的数据。例如,记事本例子应用程序使用内容提供者保存提醒。

 

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. For more information, see the Content Providers developer guide.

 

内容提供者实现为ContentProvider的子类,必须实现一组标准的API集合,使其他应用程序可以执行事务。请参考内容提供者开发者指引获取更多信息。

 

Broadcast receivers

 

广播接收器

 

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. Applications can also initiate broadcasts—for example, to let other applications 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.

 

广播接收器是响应系统范围广播公告的组件。许多广播起源于系统——例如,公报屏幕关闭、电量低、或截屏的广播。应用程序还可以初始化广播——例如,让其它应用程序知道某些数据已经被下载到设备并且对于它们是可用的。虽然广播接收器不显示用户界面,但它们可以创建状态栏通知,当广播事件发生时警告用户。虽然更一般地,广播接收器只是一个通往其它组件的“入口”,以及打算做非常少量的工作。例如,它会初始化一个服务以执行一些基于事件的工作。

 

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

 

Android系统设计的独特观点是任何应用程序可以启动另一个应用程序组件。例如,如果你想用户用设备摄像头截取一张照片,可能有另一个应用程序可以做到,而你的应用程序可以使用它而无需自己去开发一个截取照片的活动。你不需要合并甚至链接摄像头应用程序的代码。取而代之的是,你可以简单地启动摄像头应用程序的截取照片的活动。当完成时,照片会返回到你的应用程序使你能使用它。对于用户,就像摄像头真的是你的应用程序的一部分那样。

 

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. 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系统可以这样做。所以,为了激活另一个应用程序的组件,你必须传递一个消息到系统,指定你的意图以启动一个特定的组件。然后系统传递组件给你。

 

Activating Components

 

激活组件

 

Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an 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.

 

四种组件类型的三种——活动、服务和广播接收器——被一种称为意图的异步消息激活。意图在运行时把独立组件绑定在一起(你可以想象它们是信使,向其它组件请求动作),不管组件属于你的应用程序还是属于另一个。

 

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). For example, an intent might convey a request for an activity to show an image or to open a web page. 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).

 

对于活动和服务,一个意图定义之行的动作(例如,去“查看”或“发送”某些东西)并且可以指定要操作的数据的URI(组件要启动可能需要知道的其它东西)。例如,一个意图可能传送一个请求,希望活动展示一张图片或打开一个网页。某些情况下,你可以启动一个活动去接收结构,此时,活动还会用一个意图返回结果(例如,你可以产生一个意图,让用户选择个人电话簿,然后让它返回给你——返回的意图包含指向所选电话簿的URI)。(注: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").

 

对于广播接收器,意图简单地定义被广播的公告(例如,指出设备电量低的广播,仅包含一个已知动作字符串“电量低”)。

 

The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a 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. This leaves a layer of abstraction between the content provider and the component requesting information (for security).

 

另一种组件类型,内容提供者,不会被意图激活。然而,它在ContentResolver向其发出的请求时被激活。内容解析器用内容提供者处理所有直接的事务,致使你不需要组件用提供者执行事务,只要调用ContentResolver上的方法即可。这样,在内容提供者和组件请求信息之间保留一个抽象层(考虑安全性)。

 

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

 

* 你可以通过传递意图给startActivity()或startActivityForResult()(当你想让活动返回结果时)启动一个活动(或给它一些新事情做)

 

* 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().

 

* 你可以通过传递意图给startService()启动服务(或把新指令交给一个进行中的服务),或者你可以通过传递意图给bindService()绑定到服务。

 

* You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

 

* 你可以通过传递意图给诸如sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()的方法,初始化广播。

 

* You can perform a query to a content provider by calling query() on a ContentResolver.

 

* 你可以通过在ContentResolver上调用query()执行查询。

 

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.

 

请参考意图和意图过滤器文档获取更多关于使用意图的信息。更多关于激活特定组件的信息还在以下文档中提供:活动、服务、广播接收器和内容提供者。

 

The Manifest File

 

清单文件

 

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). Your application must declare all its components in this file, which must be at the root of the application project directory.

 

在Android系统可以启动应用程序组件之前,系统必须通过读取应用程序的AndroidManifest.xml文件(“清单”文件)知道组件的存在。你的应用程序必须在该文件中声明它的所有组件,那个文件必须放在应用程序工程目录的根。

 

The manifest does a number of things in addition to declaring the application's components, such as:

 

清单完成一些事情,并且声明应用程序组件,诸如:

 

* Identify any user permissions the application requires, such as Internet access or read-access to the user's contacts.

 

* 标识应用程序需要的任意用户权限,诸如互联网访问或用户电话簿的读取访问权。

 

* Declare the minimum API Level required by the application, based on which APIs the application uses.

 

* 声明应用程序需要的最小API级别,基于应用程序使用那些API。

 

* Declare hardware and software features used or required by the application, such as a camera, bluetooth services, or a multitouch screen.

 

* 声明应用程序使用或需要的硬件和软件特性,诸如摄像头、蓝牙服务,或多点触碰屏幕

 

* API libraries the application needs to be linked against (other than the Android framework APIs), such as the Google Maps library.

 

* 应用程序需要链接的API库(非Android框架API),诸如Google地图库。

 

* And more

 

* 其它

 

Declaring components

 

声明组件

 

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:

 

清单的主要任务是告诉系统应用程序的组件。例如,一个清单文件可以声明一个活动如下所示:

 

<?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属性指向标识应用程序的图标资源。

 

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子类的完整类名,而android:label属性指定一个字符串,作为活动的用户可视标签使用。

 

You must declare all application components this way:

 

你必须用这种方式声明所有应用程序组件:

 

* <activity> elements for activities

 

* <activity>元素用于活动

 

* <service> elements for services

 

* <service>元素用于服务

 

* <receiver> elements for broadcast receivers

 

* <receiver>元素用于广播接收器

 

* <provider> elements for content providers

 

* <provider>元素用于内容提供者

 

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. 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().

 

你在你的源代码中包含但不在清单中声明的活动、服务和内容提供者,对于系统来说是不可见的,因此,将永远不会被运行。然而,广播接收器可以在清单中声明,或者在代码中动态创建(作为BroadcastReceiver对象)然后通过调用registerReceiver()注册到系统中。

 

For more about how to structure the manifest file for your application, see the The AndroidManifest.xml File documentation.

 

请参考AndroidManifest.xml文件文档获取更多关于如何结构化你的应用程序的清单文件的细节。

 

Declaring component capabilities

 

声明组件功能

 

As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcast receivers. You can do so by explicitly naming the target component (using the component class name) in the intent. However, the real power of intents lies in the concept of intent actions. 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. If there are multiple components that can perform the action described by the intent, then the user selects which one to use.

 

正如前文所讨论的,在激活的组件中,你可以使用一个意图来启动活动、服务,以及广播接收器。你可以通过在意图中显式命名目标组件来做到(使用组件类名)。然而,意图的真正威力在于意图动作的概念。使用意图动作,你简单地描述你想执行的动作类型(以及可选的,你想施予动作的数据),并且允许系统在设备上查找可执行该动作的组件然后启动它。如果有多个可以执行意图所描述的动作的组件时,用户选择使用其中一个。

 

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.

 

系统区分可响应意图的组件的方法是,比较设备上其它应用程序的清单中提供的意图过滤器所接受的意图。

 

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. 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). 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().

 

例如,一个电邮应用程序有一个活动是用来写新电邮的,可以在其清单入口中声明一个意图过滤器以响应“发送”意图(以便于发送电邮)。然后你的应用程序的活动可以创建一个带“发送”动作(ACTION_SEND)的意图,系统把它与电邮应用程序的“发送”活动匹配,然后在你用startActivity()调用意图时启动它。

 

For more about creating intent filters, see the Intents and Intent Filters document.

 

请参考意图和意图过滤器文档获取创建意图过滤器的更多相关信息。

 

Declaring application requirements

 

声明应用程序最低要求

 

There are a variety of devices powered by Android and not all of them provide the same features and capabilities. 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. Most of these declarations are informational only and the system does not read them, but external services such as Google Play do read them in order to provide filtering for users when they search for applications from their device.

 

有不同种类的Android设备,它们中不是所有都提供相同的特性和功能。为了防止你的应用程序安装在缺乏你的应用程序所需特性的设备上,你清楚地通过在你的清单文件中声明设备和软件要求,以对你的应用程序支持的设备类型定义一个简介,是尤为重要的。这些声明大部分只是报告性质的,系统不会读取它们,但外部服务诸如Google Play读取它们以在用户搜索适合它们设备的应用程序时,向用户提供过滤功能。

 

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. That way, devices that do not have a camera and have an Android version lower than 2.1 cannot install your application from Google Play.

 

例如,如果你的应用程序需要一个摄像头和使用了Android 2.1(API级别7)引入的API时,你应该在你的清单文件中声明这些作为最低要求。那样的话,没有摄像头并且所拥有的Android版本号低于2.1的设备将不能从Google Play上安装你的应用程序。

 

However, you can also declare that your application uses the camera, but does not require it. 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:

 

这里有一些重要的设备特性,你应该在设计和开发你的应用程序时进行考虑。

 

Screen size and density

 

屏幕大小和密度

 

In order to categorize devices by their screen type, Android defines two characteristics for each device: screen size (the physical dimensions of the screen) and screen density (the physical density of the pixels on the screen, or dpi—dots per inch). To simplify all the different types of screen configurations, the Android system generalizes them into select groups that make them easier to target.

 

为了根据设备屏幕类型分类设备,Android对每种设备定义两个特性:屏幕大小(屏幕的物理规格)以及屏幕密度(屏幕上的像素的物理密度,或dpi——像素每英寸)。为了简化屏幕配置的所有不同类型,Android系统把它们规范化到可选择组以使它们较容易被目标化。

 

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

 

默认,你的应用程序兼容所有屏幕大小和密度,因为Android系统会合理地调整你的用户界面布局和图片资源。然而,你应该通过使用可选的布局资源,然后通过在你的清单中用<supports-screens>元素准确地声明你的应用程序支持哪些屏幕大小,以创建专门的布局给某种屏幕大小和提供专门的图片给某些密度。

 

For more information, see the Supporting Multiple Screens document.

 

请参考支持多屏幕文档获取更多信息。

 

Input configurations

 

输入配置

 

Many devices provide a different type of user input mechanism, such as a hardware keyboard, a trackball, or a five-way navigation pad. If your application requires a particular kind of input hardware, then you should declare it in your manifest with the <uses-configuration> element. However, it is rare that an application should require a certain input configuration.

 

许多设备提供不同种类的用户输入机制,诸如硬键盘、跟踪球,或一个五方向导航板。如果你的应用程序需要一个特定类型的输入硬件,那么你应该在你的清单中用<uses-configuration>元素声明它。然而,应用程序很少需要特定的输入配置。

 

Device features

 

设备特性

 

There are many hardware and software features that may or may not exist on a given Android-powered device, such as a camera, a light sensor, bluetooth, a certain version of OpenGL, or the fidelity of the touchscreen. You should never assume that a certain feature is available on all Android-powered devices (other than the availability of the standard Android library), so you should declare any features used by your application with the <uses-feature> element.

 

一个给定的Android设备上很多硬件和软件特性可能存在也可能不存在,诸如一个摄像头、光感应器、蓝牙、特定版本的OpenGL,或触摸屏的保真度。你绝不应该假设某个特性在所有Android设备上是可用的(不同于标准Android库的可用性),所以你应该用<uses-feature>声明你的应用程序所使用的任何特性。

 

Platform Version

 

平台版本

 

Different Android-powered devices often run different versions of the Android platform, such as Android 1.6 or Android 2.3. Each successive version often includes additional APIs not available in the previous version. In order to indicate which set of APIs are available, each platform version specifies an API Level (for example, Android 1.0 is API Level 1 and Android 2.3 is API Level 9). If you use any APIs that were added to the platform after version 1.0, you should declare the minimum API Level in which those APIs were introduced using the <uses-sdk> element.

 

不同Android设备往往运行不同版本的Android平台,诸如Android 1.6或Android 2.3。每个连续版本经常包含前一个版本中不可用的额外API。为了指出哪些API集合时可用的,每个平台版本指定一个API级别(例如,Android 1.0是API级别1而Android 2.3则是级别9)。如果你使用任意在版本1.0后添加到平台的API,你应该用<uses-sdk>元素声明那些API引入的最小API级别。

 

It's important that you declare all such requirements for your application, because, when you distribute your application on Google Play, the store uses these declarations to filter which applications are available on each device. As such, your application should be available only to devices that meet all your application requirements.

 

你声明你的应用程序的所有这些条件是很重要的,因为,当你在Google Play上分发你的应用程序时,商店使用这些声明根据每种设备过滤出可用的应用程序。同样,你的应用程序应该只对满足你的应用程序的所有最低条件的设备可用。

 

For more information about how Google Play filters applications based on these (and other) requirements, see the Filters on Google Play document.

 

请参考Google Play上的过滤器文档获取更多关于Google Play如何根据它们(和其它)必需条件过滤应用程序的信息。

 

Application Resources

 

应用程序资源

 

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. For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files. 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).

 

一个Android应用程序不仅仅由代码组成——它需要与源代码分离的资源,诸如图片、音频文件,以及与应用程序可视化表现相关的任意东西。例如,你应该在XML文件中定义活动用户界面的动画、菜单、样式、颜色,以及布局。使用应用程序资源简化对你的应用程序不同特性的更改,避免修改代码,并且——通过提供一系列可选资源——使你能针对各种设备配置优化你的应用程序(诸如不同语言和屏幕大小)。

 

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

 

对于你在你的Android工程内包含的每个资源,SDK构建工具定义一个唯一整型ID,你可以在你的应用程序代码中或从定义在XML内的其它资源中使用它来引用资源。例如,如果你的应用程序包含一个名为logo.png的图片文件(保存在res/drawable/目录),SDK工具生成一个名为R.drawable.logo的资源ID,你可以用它引用图片,在你的用户界面中插入它。

 

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

 

提供资源与你的源代码分离的最重要的观点之一是,让你有能力提供可选资源给不同设备配置。例如,通过在XML中定义UI字符串,你可以把字符串翻译成其它语言然后在单独的文件中保存那些东西。然后,基于你后加到资源目录名的语言限定符(诸如res/values-fr/供法语字符串值使用)以及用户语言设置,Android系统把合适的语言字符串应用到你的用户界面上。

 

Android supports many different qualifiers for your alternative resources. 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. 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. Then, the system automatically applies the appropriate layout depending on the current device orientation.

 

Android支持许多不同的限定符供你的可选资源使用。限定符是一个短字符串,你可以包含在你的资源目录的名称中,以便于定义那些资源应该被使用的设备配置。作为另一个例子,你应该经常为你的活动创建不同的布局,依赖于设备的屏幕方向与大小。例如,当设备屏幕是竖向(高屏),你可能想让一个带按钮(注:这里指多个按钮,下同)的布局是垂直的,但当屏幕是横向方向(宽屏),按钮应该是水平排列的。为了根据方向改变布局,你可以定义两个不同的布局然后应用合适的限定符到各自布局的目录名。然后,系统根据当前设备的方向自动应用合适的布局。

 

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.

 

请参考应用程序资源开发者指引获取更多关于你可以在应用程序中包含的不同种类资源,以及如何为不同设备配置创建可选资源的信息。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 3.2 r1 - 03 Oct 2011 18:28

 

Android 4.0 r1 - 08 Mar 2012 0:34

 

-------------------------------

 

patch:

1. Android 4.0 r1 - 08 Mar 2012 0:34

for activiting each type of

->

for activating each type of

 

such as Android Market do read

->

such as Google Play do read

 

install your application from Android Market.

->

install your application from Google Play.

 

declare that your applicaiton

->

declare that your application

 

on Android Market, Market uses

->

on Google Play, the store uses

 

For more information about how Android Market filters applications based on these (and other) requirements, see the Market Filters document.

->

For more information about how Google Play filters applications based on these (and other) requirements, see the Filters on Google Play document.

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证中描述的条款进行修改)

 

你可能感兴趣的:(应用程序)