Application Fundamental
Quickview
预览
- Android applications are composed of one or more application components (activities, services, content providers, and broadcast receivers)
- Android应用程序是由一个或者多个应用组件组成 (activities, services, content providers, 和 broadcast receivers)
- 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
- 在 manifest 清单文件中必须对应用程序的所有组件作公布/声明,同时也需声明所有的应用需求,比如对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
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 package / 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 开始该进程,当它不再需要或者当系统必须为其他应用程序释放内存时,关闭该进程。
- 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.
- 一个应用程序也可以申请访问设备数据的授权,比如用户的联系人,短信,可插入储存空间( SD卡),摄像头,蓝牙,及其他。所有应用权限必须在用户安装时被确认授予。
- The core framework components that define your application.
- 定义你的应用程序的代码框架组件。
- The manifest file in which you declare components and required device features for your application.
- 在你的申明组件中的 manifest file / 清单文件,以及你的应用程序对设备功能需求的定义。
- 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.
Application 组件是一个 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.
-
一个 activity / 活动 代表用户界面的一个独立屏幕。例如,一个邮件应用程序应该有一个 activity 用于显示新邮件列表,另一个 activity 用于撰写一封邮件,还有一个 activity 用于读取邮件。尽管所有 activitie 协同工作以构成邮件应用程序的用户体验,但彼此之间相对独立。应次,不同的应用程序能够从任何一个 activity 启动 (只要邮件应用程序允许)。例如,用户需要分享一张照片,一个拍照应用程序能够启动邮件应用程序的 activity 。
An activity is implemented as a subclass of
Activity
and you can learn more about it in theActivities developer guide.activity 是一个实现了
Activity
的子类,你可以在 Activities 开发者指导部分了解更多。
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.
-
service 是在后台运行,执行长时间操作或者执行远程操作。 service 不提供用户界面。例如,当用户在另一个应用程序时,一个 service 可在后台播放音乐,或者是从网络上获取数据,而不阻断用户与当前 activity 的交互。其他组件,比如一个 activity ,为了与该 service 互动,可以启动或者 绑定它。
A service is implemented as a subclass of
Service
and you can learn more about it in theServices developer guide.
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. -
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 application and not shared. For example, the Note Pad sample application uses a content provider to save notes.
Content providers 也可以用于读写你的应用程序的私有数据而非分享它。 例如, 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 applications to perform transactions. For more information, see the Content Providers developer guide.content provider 是实现了
ContentProvider
的子类,并实现一个标准API以使得其他应用程序执行交互。你可以在 Content Providers 开发者指导部分了解更多。
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.
-
broadcast receiver / 广播接收者 是一个用于回应系统范围广播通知的组件。许多广播由系统创建 — 例如,屏幕已关闭,电池电量低或者图片已保存的广播通知。应用程序也可起发起广播 — 比如,为了让其他应用程序知晓一些数据已经下载完毕并可使用。 尽管 broadcast receivers 并不会设置一个用户界面,它们仍就可以在一个广播事件发生时 create a status bar notification 以提醒用户。然而,更通常的情况是,broadcast receiver 只是一个通往其他组件的 "途径" ,并且它总是被用于完成最简单的任务。例如,它应到创建一个 service 来执行基于该事件的一些任务。
A broadcast receiver is implemented as a subclass of
BroadcastReceiver
and each broadcast is delivered as anIntent
object. For more information, see theBroadcastReceiver
class.broadcast receiver 是实现了
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 系统设计中的独特之处在于,任何应用均可以启动其他应用程序的组件。 例如,如果你希望用户使用设备摄像头拍摄一张照片,或许已经有其他的应用程序实现该任务而你的应用程序只是使用该功能,而无需重新开发自身的拍照 activity 。你不需要并入该摄像应用的代码,甚至都无需链接至该代码。 相反,你可以简单地启动该摄像应用程序内的 activity 来拍一张照片。 拍完后,照片甚至返回你的应用程序可以直接使用了。对于用户,看起来好像摄像师你的应用程序的一部分。
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 nomain()
function, for example).
当系统启动一个组件,它就为该应用程序启动了相应的进程 (即便还没有开始运行) 并对该组件所必需的类进行实例化。例如,如果你的应用程序启动了摄像应用的 activity 用于拍摄一张照片,该 activity 在属于摄像应用的进程中运行,而非你的应用程序进程中。因此,不像其他系统中的应用程序,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 系统可以。 因此为了激活其他应用程序的组件,你必须向系统发布一个消息,具体说明你的 intent 从而启动一个特定组件。然后,系统为你激活该组件。
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.
四种组件中的三种 — activities, services, 和 broadcast receivers — 是由一个叫做 intent 的异步信息激活。 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). 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).
对于 activities 和 services, 一个 intent 定义了需要执行的动作 (例如,去 "浏览" 或者 "发送" 一些内容) 并且可能指定操作所需数据的 URI (尤其是组件启动时可能需要知道的资源)。例如, 一个 intent 可能会向一个 activity 传递展示一张图像或打开一个 web 页面的请求。在一些情况下,你能够启动一个 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").
对于 broadcast receivers,intent 简单定义了需要广播的通知内容 (例如, 表示电池电量低的 broadcast 仅包括一个知名的活动字符串可显示为 "电池电量低")。
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).
其他的组件类型,content provider,不由 intents 激活。相反,它在被一个来自 ContentResolver
的请求所针对时激活。
content resolver / 内容解析器 控制了与 content provider / 内容提供商 的所有直接 交换 / 业务,因此组件没必要与提供商执行 交换 / 业务,取而代之的是呼叫 ContentResolver
对象的方法。 这就在 content provider / 内容提供商和组件请求信息之间构成了一个抽象层 (为了安全原因)。
There are separate methods for activiting each type of component:
以下是激活各种类型组件的特定方法:
- You can start an activity (or give it something new to do) by passing an
Intent
tostartActivity()
orstartActivityForResult()
(when you want the activity to return a result). - 为了启动一个 activity (或者让它执行新的任务),你可以传递一个
Intent
给startActivity()
或者startActivityForResult() 方法
(当你希望该 activity 返回一个结果) 。 - 为了启动一个 service (或者给一个正在进行中的 service 一个新的指令) ,你可以传递一个
Intent
给startService()
方法。或者你可以传递一个
service。Intent
给bindService()
方法来
绑定该 - You can initiate a broadcast by passing an
Intent
to methods likesendBroadcast()
,sendOrderedBroadcast()
, orsendStickyBroadcast()
. - 为了发布一个broadcast ,你可以传递一个
Intent
给类似
sendBroadcast()
,sendOrderedBroadcast()
, 或者sendStickyBroadcast()
的方法。
- You can perform a query to a content provider by calling
query()
on aContentResolver
. - 为了对一个 content provider 执行查询,你可以在一个
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:
manifest 完成一系列的事情用来定义该应用程序的组件,比如:
- 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 Level ,基于该应用程序所使用的 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 Maps library 。
- 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:
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.
In the <activity>
element, the android:name
attribute specifies the fully qualified class name of theActivity
subclass and the android:label
attributes specifies a string to use as the user-visible label for the 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. 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 中定义的 Activities, services, 和 content providers 对于系统不可见并且,显而易见,根本不会运行。当然了, broadcast receivers 既可以在 manifest 中定义 也可以在代码中 (使用 BroadcastReceiver 对象
) 实时创建通过呼叫 registerReceiver()
方法向系统提交注册。
For more about how to structure the manifest file for your application, see the The AndroidManifest.xml File documentation.
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.
如上所述,对于 Activating Components ,你可以使用一个 Intent
来启动 activities, services, 和 broadcast receivers。 你可以通过在该 intent 中明确地命名该目标组件 (用该组件的类名)。当然了, intents 的真正作用在于 intent 活动的意图。通过 intent 活动,你简单地表述了你希望执行的活动类型(也可选择提供你所期望能够被活动处理的数据), 并准许系统在设备中寻找并开启可以执行该活动的一个组件。如果有多个组件能够执行该 intent 所描述的活动,那么由用户选择使用那个。
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.
系统识别组件能够响应一个 intent 的方式是通过将 所接收的 intent 与 设备中其他应用程序的 manifest file / 清单文件提供的 intent filters 作比对。
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.
当你在你的应用程序 manifest 文件中定义了一个组件,你可以选择定义 intent filters / 意图过滤器,这也就定义了组件的性能使其可以响应其他应用程序的 intents 。为了给你的组件定义一个 intent filter ,你可以在组件描述元素内容内增加一个 <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 withstartActivity()
.
例如, 对于一个邮件应用,撰写新邮件的活动可能在其 manifest 文件中定义一个 intent filter 以便响应 "发送" intents (为了发送邮件)。你的应用程序中的一个 activity 可以创建一个有着“发送” 活动的 intent (ACTION_SEND
),当你通过startActivity()方法调用该 intent 时,系统可以匹配该邮件应用程序的 “发送” activity 并执行它。
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 Android Market do read them in order to provide filtering for users when they search for applications from their device.
搭载 Android 的设备各式各样,并非都具备相同的特征和性能。为了避免在缺乏所需功能的设备上安装你的应用程序,通过在你的 manifest 文件中声明硬件和软件要求来清晰定义你的应用程序支持的硬件类型是很重要的。 这些声明的大多数信息系统并不读取,但外部服务例如 Android Market 会读取这些信息以便用户在为他们的设备选择应用程序时提供一个过滤。
For example, if your application requires a camera and uses APIs introduced in Android 2.1 (API Level7), 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 Android Market.
例如,如果你的应用程序要求一个摄像头并且使用在Android 2.1 (API Level7) 中介绍的 API,那么你应该在你的 manifest 文件中声明这些要求。这样,没有摄像头或者安装了低于 Android 2.1 版本的设备就不会从 Android Market 上下载安装你的应用程序。
However, you can also declare that your applicaiton 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 系统会对你的 UI 布局和图像资源作出适当的调整。当然了, 你应当为某种屏幕大小创建特定的布局并为某种屏幕密度提供特定的图像,使用可供选择的布局资源,并在你的 manifest 文件中使用
<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. -
许多设备提供不同类型的用户输入机制,比如硬件键盘,轨迹球,或者五方向导航按键。如果你的应用程序要求一种特殊的输入硬件,那么你就应该在你的 manifest 文件中使用
<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。 In order to indicate which set of APIs are available, 每个平台版本对应一个 API Level (例如, Android 1.0 对应于 API Level 1 , Android 2.3 对应于 API Level 9)。如果你使用任何在 1.0 版之后平台新增的 API,你应该使用
<uses-sdk>
元素声明最低 API Level 是包含这些 API的。
It's important that you declare all such requirements for your application, because, when you distribute your application on Android Market, Market 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.
为你的应用程序声明所有这些要求至关重要,因为,当你在 Android Market 上发布你的应用程序时,Market 使用这些声明来过滤该应用程序是否对于每台设备可用。 这样,你的应用程序仅对能够满足你的应用程序要求的设备可用。
For more information about how Android Market filters applications based on these (and other) requirements, see the Market Filters document.
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).
一个应用程序不仅仅由代码组成 — 它需要区别于源代码的资源,比如图片,音频文件,以及任何与应用程序视觉呈现相关联的内容。例如,你应该使用 XML 文件定义动画,菜单,风格,颜色,以及 activity 用户界面的布局。 使用应用程序资源文件,可以更容易地更新你的应用程序的特性而无需修改代码,并且 — 通过提供多套可替换资源文件 — 使您能够针对各种设备配置优化你的应用程序(比如不同语言或屏幕大小)。
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 namedR.drawable.logo
, which you can use to reference the image and insert it in your user interface.
For every resource that you include in your Android project, SDK 构建工具定义一个唯一的正整数 ID 标识符,你可以使用该标识符从你的应用程序代码中或者从XML文件中定义的其他资源中特指该资源。例如,如果你的应用程序中包含一个名为 logo.png 图片文件
(保存在 res/drawable/
文件夹里), SDK 工具会生成一个资源 ID 命名为 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 字符串,你可以将这些字符串翻译成其他语言并保存在特定的文件夹中。然后,基于语言 qualifier / 修饰词 你添加资源文件夹名称 (比如 res/values-fr/
对应于法语字符串) 以及用户语言设置, Android 系统会给你的 UI 提供适当的语言字符串。
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 支持许多不同的 qualifiers / 修饰符 。修饰符是包含在你的资源文件夹名称中的一个短字符串,以便界定哪些设备配置可使用这些资源。 另一个例子,对于不同的设备屏幕和大小,你应当为你的 activities 创建不同的布局。 例如,当设备屏幕是纵向的 (高),你可能希望一个按钮垂直排列的布局,但当屏幕是横向的(宽),按钮应当水平排列。为了根据方向调整布局,你可以定义两个不同的布局文件并给每个布局文件夹提供适当的修饰符。这样,系统会根据特定的设备方向自动为其提供适当的布局。
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.