Application Fundamentals 应用程序基本原理

来源:互联网 发布:伤城影评知乎 编辑:程序博客网 时间:2024/05/18 02:31

Application Fundamentals  应用程序基础

Android applications arewritten in the Java programming language. The Android SDK tools compile thecode—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 isthe file that Android-powered devices use to install the application.

    Android应用程序是由Java编程语言写成的。Android的SDK工具编译的Java代码,包括:所有数据、资源文件一起放到一个扩展名为.apk文件里。一个完整的.apk文件被看成是一个应用程序,并能安装到android设备上的应用程序安装文件。

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

          一旦安装到设备中,每一个Android应用都生存在她自己的安全沙箱里:

·        The Android operating system is a multi-user Linux system in which eachapplication is a different user.

         Android操作系统是一个多用户的Linux系统,每一个应用是一个不同的用户。

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

          默认地,系统分配给每一个应用程序一个唯一的Linux用户ID。(ID仅被系统使用,程序自己并不知道这个ID)。在一个应用里,系统为所有文件设置许可而只有通过用户ID才能访问这些文件。

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

        每个进程有她自己的虚拟机(VM),所以某个应用在运行时,和其他应用是隔离的。

·        By default, every application runs in its own Linux process. Androidstarts the process when any of the application's components need to beexecuted, then shuts down the process when it's no longer needed or when thesystem must recover memory for other applications.

         默认地,每一个应用都运行在她自己的Linux进程中。当需要执行任何应用代码的时候,Android就会启动进程;而当该应用不再需要或系统需要为其他应用请求系统资源的时候,Android就关闭该进程。

In this way, the Androidsystem implements the principle of least privilege. That is, eachapplication, by default, has access only to the components that it requires todo its work and no more. This creates a very secure environment in which anapplication cannot access parts of the system for which it is not givenpermission.

         通过这种方法,android系统实现了最小权限原则。指默认下每个应用程序只有在某个组件需要工作时才能被访问到。这样就创建了一个安全的环境,即应用程序是不能访问那些系统中没有获得权限的部分。

However, there are ways for anapplication to share data with other applications and for an application toaccess system service

         然而还是有方法使应用能共享数据和使应用能访问系统服务:

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

         可以使两个应用程序分享同一个linux用户id,这种情况下他们能互相访问文件,为了节省系统资源,同一个用户id的应用可以在同一个linux进程中运行,分享同一个虚拟机(应用也必需有相同的签字证书)

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

         应用能请求访问设备数据的权限,例如:联系人,sms信息,存储卡(sd卡),照相机,蓝牙等等。所有应用的安装权限必需通过用户许可。

         That covers the basicsregarding how an Android application exists within the system. The rest of thisdocument introduces you to:

          以上基本的说明了下一个android应用是怎么存在在系统里的。文档还介绍了:

·        The core framework components that define your application.

         你应用中的核心框架组件。

·        The manifest file in which you declare components and required devicefeatures for your application.

         mainifest.xml文件,用来声明应用中使用的组件和对设备的特殊要求。

·        Resources that are separate from the application code and allow yourapplication to gracefully optimize its behavior for a variety of deviceconfigurations.

          资源文件,是从应用程序代码中分离出来的,其能使你的应用能在多样化的设备配置下完美有效的展示出来。

ApplicationComponents 应用程序组件

Application components are theessential building blocks of an Android application. Each component is adifferent point through which the system can enter your application. Not allcomponents 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 aunique building block that helps define your application's overall behavior.

应用程序组件是构建Android应用程序的必要模块。每个组件是一个不同的切入点,通过它系统能进入你的应用程序。对于用户来说不是所有的组件都是真实的切入点,有些组件还彼此相互依赖。但是每个组件作为实体单独的存在着并扮演一个特殊的角色———— 每一个组件都是独特的构造模块,一起来定义你应用程序所有的行为。

There are four different typesof application components. Each type serves a distinct purpose and has adistinct lifecycle that defines how the component is created and destroyed.

有四种不同类型的应用程序组件。每种类型服务于不同的目的,并且有不同的生命周期来定义组件的创建和销毁。

Here are the four types ofapplication components:

这有四种类型的应用程序组件:

Activities

An activity represents a singlescreen with a user interface. For example, an email application might have oneactivity that shows a list of new emails, another activity to compose an email,and another activity for reading emails. Although the activities work togetherto form a cohesive user experience in the email application, each one isindependent of the others. As such, a different application can start any oneof these activities (if the email application allows it). For example, a cameraapplication can start the activity in the email application that composes newmail, in order for the user to share a picture.

一个activity 描绘一个用户界面。例如,一个邮件应用可能有一个显示新邮件列表的activity,一个组建邮件的activity,一个阅读邮件的activity。虽然这些activity在一个邮件应用程序中协同合作来形成一个完整的用户体验,但每一个activity和其它的activity是相互独立的。就像这样,一个其他的应用程序可以启动以上任何一种activity(如果邮件应用程序同意的话)。例如,一个照相应用程序可以启动邮件应用中的activity来组成新的邮件,这样能使用户分享图片。

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

一个activity是一个Activity对象的子类,你可以在开发指南Activity中学到更多。

Services

service is a component that runs inthe background to perform long-running operations or to perform work for remoteprocesses. A service does not provide a user interface. For example, a servicemight play music in the background while the user is in a differentapplication, or it might fetch data over the network without blocking userinteraction with an activity. Another component, such as an activity, can startthe service and let it run or bind to it in order to interact with it.

一个service是一个组件,运行在后台来执行长时间操作或执行远程操作。service不提供用户界面。例如,一个service可以使用户在其他应用里时,同时在后台播放音乐,或可以在不阻塞用户在activity界面中交互时从网络中取得数据。另一种组件,比如说一个activity可以启动一个service并运行起来或和activity绑定以便交互。

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

一个service是一个Service对象的子类,你可以在开发指南Service中学到更多。

Content providers

content provider manages a sharedset of application data. You can store the data in the file system, an SQLitedatabase, on the web, or any other persistent storage location your applicationcan access. Through the content provider, other applications can query or evenmodify the data (if the content provider allows it). For example, the Androidsystem provides a content provider that manages the user's contact information.As such, any application with the proper permissions can query part of thecontent provider (such as ContactsContract.Data) to read and write information about a particularperson.

content provider管理一个分享式的应用数据集合。你可以存储数据在文件系统,SQLLite数据库,网络上,或你的应用可以访问到的任何本地持久化存储容器里。通过content provider,其他应用可以查询或甚至更新数据(如果content provider允许)。例如,android系统提供一个contentprovider来管理用户的联系人信息,这样,任何拥有适当权限的应用可以查询contentprovider中部分内容(如 ContactsContract.Data)来读取和写入一个特定人的信息。

Content providers are alsouseful for reading and writing data that is private to your application and notshared. For example, the Note Pad sample application usesa content provider to save notes.

contentprovider也可以读和写那些仅对你的应用可见的数据并不提供共享。例如,记事本例子应用使用contentprovider来保存笔记。

A content provider isimplemented as a subclass of ContentProvider and must implement a standard set of APIs thatenable other applications to perform transactions. For more information, seethe Content Providers developer guide.

一个contentprovider是ContentProvider对象的一个子类,必须实现一个标准的API集合使其他应用来处理事务。要获得更多信息,请阅读ContentProviders开发指南。

Broadcast receivers

broadcast receiver is a componentthat responds to system-wide broadcast announcements. Many broadcasts originatefrom the system—for example, a broadcast announcing that the screen has turnedoff, the battery is low, or a picture was captured. Applications can alsoinitiate broadcasts—for example, to let other applications know that some datahas been downloaded to the device and is available for them to use. Althoughbroadcast receivers don't display a user interface, they may create a status bar notification to alert the user when abroadcast event occurs. More commonly, though, a broadcast receiver is just a"gateway" to other components and is intended to do a very minimalamount of work. For instance, it might initiate a service to perform some workbased on the event.

broadcast receiver是一个组件来响应系统范围内的广播通告。许多的广播来自于系统,例如,一个广播通告了:屏幕关闭,电量不足或拍了一张照片。应用也可以发布广播,例如,让其他应用知道一些数据被下载到设备中并可以使用,虽然broadcastreceiver没有一个用户界面,但他们可以创建一个状态栏通知来告知用户一个广播事情在发生。非常普遍的,一个broadcastreceiver对于其他组件来说,仅仅是一个通道并用来去做极少数量的工作。例如,它可能用来开启一个服务(service)来执行基于事件的一些工作。

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

broadcast receiver实例是BroadcastReceiver对象的一个子类,并且每一个广播做为Intent对象来传递。要获得更多信息,请阅读BroadcastReceiver类

A unique aspect of the Androidsystem design is that any application can start another application’scomponent. For example, if you want the user to capture a photo with the devicecamera, there's probably another application that does that and yourapplication can use it, instead of developing an activity to capture a photoyourself. You don't need to incorporate or even link to the code from thecamera application. Instead, you can simply start the activity in the cameraapplication that captures a photo. When complete, the photo is even returned toyour application so you can use it. To the user, it seems as if the camera isactually a part of your application.

android系统一个特性就是任何应用都可以调用其他应用的组件。例如,如果你想让用户使用设备的摄像头来拍张照片,也许另一个应用可以这么做而你的应用可以调用它,而不是开发一个你自己的activity来完成拍照功能。你不需要合并或甚至从拍照应用中调用代码。取而代之,你可以简单的启动一个activity在照相机应用中来拍照。当结束时,照片传回到你的到你的应用以便你可以使用它。对于用户来说,它看起来就像照相应用就是你的应用的一部分。

When the system starts acomponent, it starts the process for that application (if it's not alreadyrunning) and instantiates the classes needed for the component. For example, ifyour application starts the activity in the camera application that captures aphoto, that activity runs in the process that belongs to the cameraapplication, not in your application's process. Therefore, unlike applicationson most other systems, Android applications don't have a single entry point(there's no main() function, for example).

当系统启动一个组件,它就为应用开启一个进程(如果组件没用运行时)并实例化组件所需要的类。例如,如果你的应用启动一个拍照应用中的activity来拍了张照,这个activity在进程中运行,这个进程属于拍照应用而不是你的应用进程中。因此,不像其他大多数系统中的应用一样,android应用没有一个单独的切入点。(如没有main()方法)

Because the system runs eachapplication in a separate process with file permissions that restrict access toother applications, your application cannot directly activate a component fromanother application. The Android system, however, can. So, to activate acomponent in another application, you must deliver a message to the system thatspecifies your intent to start a particular component. Thesystem then activates the component for you.

因为系统运行每一个应用在单独的进程中,并带有文件权限来限制访问其他应用。你的应用不能直接激活其他应用中的组件。android系统却可以。因此为了激活其他应用中的组件,你必须向系统中传递一个消息来指定你的intent去启动特殊的组件。系统会为你激活这个组件。

ActivatingComponents  激活组件

Three of the four component types—activities,services, and broadcast receivers—are activated by an asynchronous messagecalled an intent. Intents bind individual components to each otherat runtime (you can think of them as the messengers that request an action fromother components), whether the component belongs to your application oranother.

四种组件中的三种:activities, services, 和broadcast receivers被称为intent的异步消息激活。在运行时,intent把一个个组件相互绑定起来(你可以把他们想成是消息,来请求来自其他组件的一个动作)不管这个组件是属于你的应用或其他的。

An intent is created withan Intent object, which defines a message to activateeither a specific component or a specific type of component—anintent 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). Forexample, an intent might convey a request for an activity to show an image orto open a web page. In some cases, you can start an activity to receive aresult, in which case, the activity also returns the result in an Intent (for example, you can issue an intent to let theuser pick a personal contact and have it returned to you—the return intentincludes a URI pointing to the chosen contact).

对于activity和service,intent定义了处理事件的动作(如,观看或发送某些东西),可以指定要演示的数据的地址(组件在启动时要先知道其他的一些事情)。例如,intent可以传递一个请求,需要activity来显示图片或打开一个网页。在一些情况,你可以启动一个activity来收到一个回复,同样的情况,activity可以把返回的结果放到intent对象里(例如,你可以发布一个intent让用户挑选一个人员联系表,然后返回给你,这个返回的intent中包括指向选择的联系人的地址)

For broadcast receivers, theintent simply defines the announcement being broadcast (for example, abroadcast to indicate the device battery is low includes only a known actionstring that indicates "battery is low").

对于broadcastreceviver,intent仅仅定义了广播着的通告(例如,一个广播指示设备的电量偏低,他仅包括一个显示“电量不足”的通知字符串)

The other component type,content provider, is not activated by intents. Rather, it is activated whentargeted by a request from a ContentResolver. The content resolver handles all direct transactionswith the content provider so that the component that's performing transactionswith the provider doesn't need to and instead calls methods on the ContentResolver object. This leaves a layer of abstractionbetween the content provider and the component requesting information (forsecurity).

其他的组件类型,content provider,不被intent激活。然而,它被一个来自ContentResolver的请求作为目标激活。content resolver处理所有有content provider直接提供的事务,以使组件处理事务时不需要contentprovider而调用ContentResolver对象中的方法。这样在content provider和组件请求信息时下一个抽象层来提供安全。

There are separate methods foractiviting each type of component:

以下是激活各种组件的一些方法:

·        You can start an activity (or give it something new to do) by passingan Intent to startActivity() or startActivityForResult() (when you want theactivity to return a result).

 你可以启动一个新的activity(或让其做一些新的任务)通过想startActivity()startActivityForResult() (当你activity返 回结果时)方法传递一个intent对象

·        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()方法中传递Intent对象或你可以通过想 bindService()方法中传递一个Intent来绑定服务

 

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

你可以发布一个广播,通过向 sendBroadcast()sendOrderedBroadcast()sendStickyBroadcast()方法中传递一个intent对象

 

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

你可以处理一个查询通过在ContentResolver对象中调用query()方法

For more information aboutusing intents, see the Intents and Intent Filters document. Moreinformation about activating specific components is also provided in thefollowing documents: Activities,Services, BroadcastReceiver and Content Providers.

关于使用intent的更多信息,阅读Intents and Intent Filters文档。更多关于激活特定组件的信息在以下文档中提供:Activities,Services,BroadcastReceiver 和Content Providers.

The ManifestFile

Before the Android system canstart an application component, the system must know that the component existsby reading the application's AndroidManifest.xml file (the "manifest" file). Yourapplication must declare all its components in this file, which must be at theroot of the application project directory.

在安卓系统启动应用程序组件之前,系统必须预读应用程序中的AndroidManifest.xml文件来知道有哪些组件。在你的应用程序中所有的组件必须声明在这个文件中,此文件放在应用程序的根目录下。

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

在manifest文件里,同时也申明应用程序组件的附带属性和内容,如:

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

确认应用程序所需要的所有用户许可,如:网络连接,用户联系人的阅读权限。

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

声明了程序需要的最低API版本,根据程序所使用的API来确定。

·        Declare hardware and software features used or required by theapplication, such as a camera, bluetooth services, or a multitouch screen.

声明程序所需要和使用的软硬件,如:摄像头,蓝牙,多点触摸

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

程序所需要连接的其他的API库(除了安卓核心API),如,谷歌地图API库

·        And more

等等

Declaringcomponents   声明组件

The primary task of themanifest is to inform the system about the application's components. Forexample, a manifest file can declare an activity as follows:

Mainfest文件的基本任务就是声明了程序所涉及的组件。例如,mainfest可以像下面那样声明一个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 thatidentifies the application.

在<application>元素里,android:icon属性用一个图标来标识那个程序。

In the <activity> element, the android:name attribute specifies the fully qualified classname of the Activity subclass and the android:label attributes specifies a string to use as theuser-visible label for the activity.

在<activity>里,android:name属性指明Acitvuty子类的全称,android:label属性显示了一个用户可见的activity标题栏中的标题。

You must declare allapplication components this way:

你必须使用下面的元素声明所有程序的组件。

·        <activity> elements for activities

<activity>元素声明activity组件

·        <service> elements for services

<service>元素声明services组件

·        <receiver> elements for broadcast receivers

<receiver>元素声明broadcast receiver组件

·        <provider> elements for content providers

<provider>元素声明content providers组件

Activities, services, andcontent providers that you include in your source but do not declare in themanifest are not visible to the system and, consequently, can never run.However, broadcast receivers can be either declared in the manifest or createddynamically in code (as BroadcastReceiver objects) and registered with the system bycalling registerReceiver().

Activity,services和content provider三种组件如果没有在mainfest文件中声明,那么系统是识别不了,因此也运行不了。然而,broadcastreceiver不仅可以在mainfest文件中声明,而且可以在代码中动态创建(像BroadcastReceiver对象)并通过调用registerReceiver()方法来注册。

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

如需要了解更多关于构建mainfest文件的信息,可以参阅AndroidManifest.xml文档。

Declaringcomponent capabilities  声明组件的性能

As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcastreceivers. You can do so by explicitly naming the target component (using thecomponent class name) in the intent. However, the real power of intents lies inthe concept of intent actions. With intent actions, you simply describe thetype of action you want to perform (and optionally, the data upon which you’dlike to perform the action) and allow the system to find a component on thedevice that can perform the action and start it. If there are multiplecomponents that can perform the action described by the intent, then the userselects which one to use.

就像在激活组件段落中讨论的,你可以使用Intent来启动activity,service,broadcast receiver。你可以使用明确的目标组件名称(使用组件类名)作为Intent的参数来激活他们。Intent真正的作用体现在intent对象里。在intent类中,你简单的描述你想要处理的动作并且允许设备里的系统能发现并运行起来。如果通过intent对象描述了许多组件可以运行,用户选择一个来使用。

The way the system identifiesthe components that can respond to an intent is by comparing the intentreceived to the intent filters provided in the manifest fileof other applications on the device.

系统识别组件的方法是通过intent过滤器,intent过滤器写在manifest文件里。

When you declare a componentin your application's manifest, you can optionally include intent filters thatdeclare the capabilities of the component so it can respond to intents fromother applications. You can declare an intent filter for your component byadding an <intent-filter> element as a child ofthe component's declaration element.

当在你的mainfest文件中声明组件时,你可以随意的包括阐述组件能力的intent过滤器,这样可以回复来自其他应用的intent。你可以给你的组件添加intent过滤器,通过在各自组件的元素下使用<intent-filter>元素下来实现。

For example, an emailapplication with an activity for composing a new email might declare an intentfilter in its manifest entry to respond to "send" intents (in orderto send email). An activity in your application can then create an intent withthe “send” action (ACTION_SEND), which the system matches to the email application’s“send” activity and launches it when you invoke the intent withstartActivity().

例如,一个邮件程序用来组建一个新的邮件程序可能会在它的mainfest文件里写上intent过滤器,来发送intent。在你的程序里,一个action可以创建一个发送intent消息的action,当你调用startActivity()方法中的intent时,系统会匹配邮件应用中的发送action,并启动它。

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

要获得更多关于intent过滤器的信息,查阅Intent and Intent Fileters文档。

Declaringapplication requirements  说明应用的需求

There are a variety of devicespowered by Android and not all of them provide the same features andcapabilities. In order to prevent your application from being installed ondevices that lack features needed by your application, it's important that youclearly define a profile for the types of devices your application supports bydeclaring device and software requirements in your manifest file. Most of thesedeclarations are informational only and the system does not read them, butexternal services such as Android Market do read them in order to providefiltering for users when they search for applications from their device.

使用安卓系统的设备有很多种,不是所有的设备都有相同特性和能力。为了阻止你的应用安装在那些缺少你的程序所需要的属性的设备中时,重要的是,你清晰的定义你的应用所支持的各种设备大概的样子,可以通过在你的mainfest文件里说明设备和软件所需要的东西。大多数这些描述只是信息,系统不会解析他们,但是在其他的服务中,如安卓市场,会阅读他们,在用户在他们自己的设备中查找应用时,会过滤掉那些不能在设备中运行的应用。

For example, if yourapplication requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declarethese as requirements in your manifest file. That way, devices that donot havea camera and have an Android version lower than 2.1 cannotinstall your application from Android Market.

例如,如果你的应用需要摄像头和使用的安卓2.1版本的API,你需要在你的mainfest文件中说明所需要的这些。这样,设备没有摄像头和安卓API版本比2.1还低,那么就不能从安卓市场中安装了。

However, you can also declarethat your applicaiton uses the camera, but does not require it.In that case, your application must perform a check at runtime to determine ifthe device has a camera and disable any features that use the camera if one isnot available.

然而,你也可以说明你的应用需要摄像头,但是并不需要它。在这种情况下,你的应用必须在启动时检查,如果有摄像头则用如果没有则不用。

Here are some of the importantdevice characteristics that you should consider as you design and develop yourapplication:

有一些重要的设备特性需要你在设计和开发你的应用时需要考虑的:

Screen size and density 屏幕的尺寸和像素

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

为了通过屏幕的种类来区分设备,安卓为每个设备定义了两种特性:屏幕尺寸(屏幕的物理大小)和屏幕的像素(在屏幕里的物理像素或每英寸点数)。为了简化所有不同种类屏幕的配置,安卓系统把他们做了分组:

The screen sizes are: small,normal, large, and extra large.

屏幕的尺寸:small,normal,larger,extra large
The screen densities are: lowdensity, medium density, high density, and extra high density.

尺寸像素:low density, medium density, high density, extra high density.

By default, your applicationis compatible with all screen sizes and densities, because the Android systemmakes the appropriate adjustments to your UI layout and image resources.However, you should create specialized layouts for certain screen sizes andprovide specialized images for certain densities, using alternative layoutresources, and by declaring in your manifest exactly which screen sizes yourapplication supports with the <supports-screens> element.

默认的,你的应用是匹配所有的屏幕的尺寸和像素的,因为安卓系统会根据你的界面布局和图像进行适当的调整。然而,你必须为确切的屏幕尺寸创建专门布局,为确切的屏幕像素确定专门的图片,使用备用的布局资源文件,并在你的mainfest文件中,使用<supports-screens>元素准确的说明你的应用所支持的屏幕大小。

For more information, seethe Supporting Multiple Screens document.

更多的信息,查阅SupportingMultiple Screens 文档。

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 yourapplication requires a particular kind of input hardware, then you shoulddeclare it in your manifest with the <uses-configuration> element. However, it israre that an application should require a certain input configuration.

许多设备提供不同类型的用户输入机制,例如键盘,滚动球,或五方向的导航键。如果你的应用需要一个特殊的输入机制,那么你需要在你的mainfest文件中的<uses-configuration>元素中说明。然而,一个应用很少会需要一个输入配置语句。

Device features 设备功能

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

在一个具体的安卓设备中,会存在或不存在很多软硬件功能,例如:摄像头,光传感器,蓝牙,某个版本的OpenGL,或触摸屏。你永远不能假设所有的安卓设备上会有哪些功能(除了标准的安卓类库),因此你需要使用<uses-feature>元素来说明你的应用所需要的功能。

Platform Version 平台版本

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

不同的安卓设备上运行的经常是不同的安卓版本,如,安卓1.6、2.3。后来的版本中添加的API库在以前的版本中是不支持的。为了指明那个API可用,每个平台版本有一个API等级。(如,1.0版本的API等级为1,2.3版本的API等级为9)。如果你使用1.0以后的版本,你应该在<uses-sdk>元素中说明程序需要的最小API等级。

It's important that youdeclare all such requirements for your application, because, when youdistribute your application on Android Market, Market uses these declarationsto filter which applications are available on each device. As such, yourapplication should be available only to devices that meet all your applicationrequirements.

重要的是,你要说明你的应用所需要那些支持,因为,当你在安卓市场中介绍你的应用时,安卓市场会通过这些描述来匹配哪些设备适合你的应用。这样以来,你的应用就只能在匹配你的程序的需求的设备里运行了。

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

更多的关于安卓市场怎么匹配你的应用需求的信息,参阅Market Filters文档。

ApplicationResources  应用程序资源文件

An Android application iscomposed of more than just code—it requires resources that are separate fromthe source code, such as images, audio files, and anything relating to thevisual presentation of the application. For example, you should defineanimations, menus, styles, colors, and the layout of activity user interfaceswith XML files. Using application resources makes it easy to update variouscharacteristics of your application without modifying code and—by providingsets of alternative resources—enables you to optimize your application for avariety of device configurations (such as different languages and screensizes).

一个安卓应用不仅仅是由代码组成的,它需要其他的一些资源,如:图片,音频文件和任何与显示外观等有关系的文件。例如,你需要定义动画,菜单,样式,颜色和界面的布局,使用XML文件形式。使用应用程序资源文件,可以很容易的修改你应用的一些属性而不需要修改代码,通过可替换的资源文件集合,使你能随意的匹配各种样式的设备(就像不同的语言和屏幕尺寸)。

For every resource that youinclude in your Android project, the SDK build tools define a unique integerID, which you can use to reference the resource from your application code orfrom other resources defined in XML. For example, if your application containsan image file named logo.png (saved in the res/drawable/ directory), the SDK tools generate a resource IDnamedR.drawable.logo, which you can use toreference the image and insert it in your user interface.

在你安卓项目中所包括的各种资源文件,SDK构建工具定义唯一的ID,这样你就可以在你的程序代码中引用他们。例如,如果你的应用里有个命名为logo.png的图片文件(保存在 res/drawable/ 目录下),SDK工具就会生成一个叫做R.drawable.logo的资源ID,这样你可以引用这个图片并把他加入你的用户界面里。

One of the most importantaspects of providing resources separate from your source code is the abilityfor you to provide alternative resources for different device configurations.For example, by defining UI strings in XML, you can translate the strings intoother languages and save those strings in separate files. Then, based on alanguage qualifier that you append to the resource directory'sname (such as res/values-fr/ forFrench string values) and the user's language setting, the Android systemapplies the appropriate language strings to your UI.

重要的一点就是,你需要为不同设备配置在你的程序资源里面提供各种样式的文件。例如,在XML文件中定义UI名称,你可以把这个UI名称翻译成其他语言并保存在其他文件里。这样,基于语言名称,你就可以根据你的语言设置来加载不同的语言。(如res/values-fr下的法语)。

Android supports manydifferent qualifiers for your alternative resources. Thequalifier is a short string that you include in the name of your resourcedirectories in order to define the device configuration for which thoseresources should be used. As another example, you should often create differentlayouts for your activities, depending on the device's screen orientation andsize. 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 inlandscape orientation (wide), the buttons should be aligned horizontally. Tochange the layout depending on the orientation, you can define two differentlayouts and apply the appropriate qualifier to each layout's directory name.Then, the system automatically applies the appropriate layout depending on thecurrent device orientation.

安卓系统为可相互替换的资源文件提供了许多不同的修饰符。这些修饰符是短词,把他们包括在目录名称里,以便对不同的设备配置匹配不同的文件。另一个例子,你经常会为你不同activity基于设备的屏幕方向和大小提供不同布局。例如,当设备的屏幕是高度方向的,你可能使按钮垂直排列。但当屏幕是长度方向的,你可以定义两种布局,并根据名称来选择哪个。系统自定义提供适当的布局来匹配对应的设备。

For more about the differentkinds of resources you can include in your application and how to createalternative resources for various device configurations, see the Application Resources developer guide.

更多的关于你的应用中可以包括的不同种的资源和如何创建根据设备配置来选择的资源文件,参阅Application Resources帮助文档。

 

原创粉丝点击