Application Fundamentals應用基礎

Application Fundamentals應用基礎

Android applications are written in the Java programming language. Android應用是用Java 語言寫的

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

  • Android SDK工具編譯代碼---及與代碼一塊兒的任意數據,資源文件---到一個包中,一個.apk後綴的歸檔文件.linux

    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

  • 全部代碼都在單個.apk文件中,當成一個應用,這個文件就是一般安裝在Android設備中的應用web

  • 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.因此一個應用代碼的運行,與其餘應用代碼的運行是隔離的.
    • 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. 經過這種方法,Android系統實現了最小特權原則api

    That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.安全

  • 默認,每一個應用僅僅訪問須要工做的組件,並很少作其餘的事。這樣建立了一個很是安全的環境,應用不能訪問系統沒有受權的其餘部分網絡

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

  • 然而,應用能夠有多種方法來與其餘應用,共享數據及訪問系統服務框架

    • It's possible to arrange for two applications to share the same Linux user ID, 有可能安排兩個應用共用一個linux系統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).
    • 爲了節約系統資源,捅用相同用戶ID的應用,可能也被安排運行在同一個Linux進程中並共享相同的VM(應用必須被簽名成一樣的認證)
    • 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 card),攝像頭,藍牙等,全部應用的權限必須在用戶安裝時被許可

    That covers the basics regarding how an Android application exists within the system. 上述了一個應用怎樣存在於一個系統中的相關基本概念,

    The rest of this document introduces you to:這個文檔的其餘部分將向你介紹

    • The core framework components that define your application.定義在你的應用中核心框架組件
    • The manifest file in which you declare components and required device features for your application.在manifest中,給你的應用,聲明組件及設備特色請求
    • 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. 應用組件是構建Android應塊的關鍵。                                                                                  

  • Each component is a different point through which the system can enter your application.每一個組件是一個不一樣的入點,系統能夠從這些點進入到你的應用。                                  

  • Not all components are actual entry points for the user and some depend on each other, 對於用戶來講,並非每一個組件都是實際的入點,但它們之間有一些依賴

  • but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your application's overall behavior.可是每個存在的組件都有它本身的一個入點,並扮演一個特定的角色--每個都是獨一無二的構建塊,幫助你定義你的應用的總體行爲

  • There are four different types of application components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.有四個不一樣類型的應用組件,每一個類型服務於一個不一樣的目的,並有不一樣的生命週期,生命週期定義瞭如何建立和銷燬它.

    Here are the four types of application components:下面是四種應用組件

    Activities活動
    An activity represents a single screen with a user interface. 一個 activity 在一個屏幕,顯示一個用戶接口
    For example, an email application might have one activity that shows a list of new emails, 好比,一個email應用可能有一個activity,這個activity用於顯示新的email列表
    another activity to compose an email, and another activity for reading emails. 而另外一個activity用於寫郵件,還有一個activity用於讀取郵件
    Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others.
    雖然這些activities一塊兒工做於email應用中,造成一個完整的用戶體驗但每個部分又是相互獨立的.
    As such, a different application can start any one of these activities (if the email application allows it).
    正因如此,不一樣的應才能啓動這些活動的任意一個(如個email應用容許它)
    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.
    好比,一個照相的應用,能開啓一個email應用中寫封新郵件的活動,讓用戶分享一張照片

    An activity is implemented as a subclass of Activity and you can learn more about it in the Activities 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. 一個service是長期運行在後臺,執行操做的組件,甚至能夠爲遠程進程工做
    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. 其餘的組件,好比一個activity,能夠啓動一個服務,並可讓它運行或者邦定到這個activity,以便與其進行交互操做.

    A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.一個服務是做爲Service子類來實現的,在Services開發南中你能學到更多關於它的使用

     

    Content providers 內容提供者
    A content provider manages a shared set of application data. 一個content provider管理共享的應用數據集
    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.
    你能夠把數據存在文件系統中,一個SQLite數據庫中,網上,或你應用能夠訪問的永久存儲器中
    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. 好比,Android系統提供一個內容提供者管理用戶通訊錄信息
    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. 所以,任何捅用適當權限的應用,能夠查詢內容提供者的部分來(好比 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.內容提供者對於讀取和寫入屬於你的應用的私有的非共享數據也是很是有用的,好比 Note Pad樣例應用程序,就使用內容提供者來保存筆記的

    A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other applications to perform transactions.

    一個內容提供者被看成的子類實現,而且必須實現一套標準的APIs,以讓其餘的應用能執行交換操做。

    For more information, see the Content Providers developer guide.參考Content Providers開發指南,以瞭解更多信息.

     

    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.
    但更多狀況下,一個廣播接收者只是一個其餘組件,想要作極小量事件的一個"gateway」(途徑)
    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.

  • 任何一個應用能啓動另外一個其餘應用的組件,是Android系統設計獨一無二的方面(aspect).

  • For example, if you want the user to capture a photo with the device camera, 好比,你想要用設備的照相機拍一張圖片

  • there's probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself. 其餘的應用已經有了這個功能,而且你的應用可使用它,而不須要你本身去開發一個拍照相的activity.

  • You don't need to incorporate or even link to the code from the camera application. Instead, you can simply start the activity in the camera application that captures a photo.

  • 你並不須要合併(包含)或者甚至是連接camera應中的代碼; 而只是,簡單的啓動camera應用中的活動,來拍照就能夠了.

  • When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application

  • 當拍照完成,甚至把照片返回給你的應用,因此你能使用它。對於用戶來說,camera像是你應用中一部分

  • When the system starts a component, it starts the process for that application (if it's not already running) and instantiates the classes needed for the component.

  • 當系統開啓一個組件時,它會啓動那個應用的進程(若是該應用沒有運行),並實例化該組件所須要的類.

  • For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process.

  • 舉例,若是你的應用開啓一個camera應用的activity,來拍照,這個activity將運行在屬於camera應用的進程中,而不是在你的應用的進程中

  • Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main() function, for example).

  • 所以,不像大多數其餘的系統的應用,Android應用,沒有單個的入點(好比沒有main()函數)

    Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component from another application.

  • 由於系統運行的每一個應用,在一個帶有文件權限的,獨立的進程中,這樣限制了對其餘應用的訪問,你的應用不能直接訪問其餘應用中的組件

  • The Android system, however, can. So, to activate a component in another application, you must deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you.

  • 但時,Android系統也能激活其餘應用的組件.你必須傳一個消息給系統,指定你想要啓動的組件,而後系統爲你激活這個組件

     

    Activating Components激活組件

    Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent.

    4個組件中的其中三個組件---activities,serivces,和broadcast receivers----是被叫作intent的異步消息激活的.

    Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another.在運行時,Intents把某個的組件與其餘的組件互相邦定,而無論這個組件是否屬於你的應用仍是其餘的應用(你能夠把它們想像成一個消息,用於請求一個其餘組件的動做)

    An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.一個intent是一個由Intent建立的對象.該對象定義了一個激活某個特定組件或者某個組件類型的消息,一個intent能夠是顯示的,一樣,也能夠是隱式的.

    For activities and services, an intent defines the action to perform (for example, to "view" or "send" something) and may specify the URI of the data to act on (among other things that the component being started might need to know).

    對於activities和services,一個intent(意圖)定義了一個要執行的動做(好比:to」view」或"send" 些什麼),並指定了要採用的URI格式的數據(其中一些,是其餘組件啓動所須要知道的)

    For example, an intent might convey a request for an activity to show an p_w_picpath or to open a web page.

    好比,一個intent可能傳送一個請求給一個activity,要顯示一張圖片或打開一個網頁

    In some cases, you can start an activity to receive a result, in which case, the activity also returns the result in an Intent (for example, you can issue an intent to let the user pick a personal contact and have it returned to you—the return intent includes a URI pointing to the chosen contact).

    在有些狀況,你啓動一個activity接收一個結果,這種狀況下,activity將在Intent返回一個結果.(好比,你能夠指示一個intent,讓用戶取一我的的聯繫方式,並返回給你,返回的intent中會包含一個指向選定聯繫方式的URI.)

    For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates "battery is low").對於廣播接收者,intent只是定義了一個作爲廣播的公告.(好比,一個廣播指出,設備電池低,它只是包含了一個動做字串,表示」電池低」)

    The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver.

    其餘組件,內容提供者,不會被intents所激活.進一步講,它是內容解釋者(ContentResolver)所請求的目標所激活的

    The content resolver handles all direct transactions with the content provider 內容解釋者,處理全部與內容提供者的直接交換

    so that the component that's performing transactions with the provider doesn't need to and instead calls methods on the ContentResolver object. 因此組件不須要執行與提供者交換,而是調用ContentResolver對象方法.(這一句很差理解。)

    This leaves a layer of abstraction between the content provider and the component requesting information (for security).爲了安全起見,組件請求信息與內容提供者之間有一個抽象層.

    There are separate methods for activiting each type of component:下面是激活各類類型組件的幾個方法

    For more information about using intents, see the Intents and Intent Filters document.關於使用intents的詳細信息,請看ntents and Intent Filters 文檔

    More information about activating specific components is also provided in the following documents: Activities, Services, BroadcastReceiver and Content Providers.

    在後面的文檔中,也有一些關於激活某個組件的信息Activities, Services, BroadcastReceiver and Content Providers.

     

    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). 在Android系統開啓一個應用組件以前,系統必須經過讀取AndroidManifest.xml文件來知道組件的存在

    Your application must declare all its components in this file, which must be at the root of the application project directory.

    你的應用必須把它全部的組件聲明在這個文件中,而且必須在應用工程的根目錄下

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

    這個manifest文件除了聲明組件外,還處理了許多其餘的事情,好比

    • 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庫,好比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.在<application> 元素中,android:icon指定應用的icon資源

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

    <activity> 元素中的,android:name 屬性,指定Activity子類的徹底類名

    You must declare all application components this way:你必須這樣聲明 全部應用的組件

    • <activity> elements for activities 聲明活動的元素
    • <service> elements for services 聲明服務的元素
    • <receiver> elements for broadcast receivers 聲明廣播接收者元素
    • <provider> elements for content providers 聲明內容提供者元素

    Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run.

    在你代碼中包含的,Activites,services和內容提供者,若沒有在manifest中聲明,對系統來講是不可見的,即將永遠不會運行

    However, broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling registerReceiver().可是,廣播接收者便可以在manifest中聲明,也能夠在代碼中動態建立(作爲BroadcastReceiver對象)而且經過registerReceiver()方法向系統註冊

    For more about how to structure the manifest file for your application, see the The AndroidManifest.xml File documentation. 瞭解manifest文件的詳細構建過程,請看The AndroidManifest.xml File文檔

     

    Declaring component capabilities 聲明組件功能

    As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcast receivers.

    就如在上面的Activating Components中所討論的,你能夠用一個Activating Components啓動activities,services和broadcast 接收者.

    You can do so by explicitly naming the target component (using the component class name) in the intent. 你也能夠在intent中顯式的指定目標組件(使用組件類名)

    However, the real power of intents lies in the concept of intent actions. 然而,intent真正強大的是它的intent action.(動做)

    With intent actions, you simply describe the type of action you want to perform (and optionally, the data upon which you’d like to perform the action) and allow the system to find a component on the device that can perform the action and start it.

    經過使用intent動做,你只須簡單的描述你要執行的action類型,(而且,可選的與執行動做有關的數據),而且容許系統在設備上找到一個組件,這樣就能夠執行那個動做並啓動它

    If there are multiple components that can perform the action described by the intent, then the user selects which one to use.

    若是有多個組件能夠執行,intent指定的action,那麼用戶選擇執行那一個.

    The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other applications on the device.

    經過比較設備上的其餘應用的manifest文件上的intent filters與接收到的intent.系統肯定那個組件能夠響應一個intent.

    When you declare a component in your application's manifest, you can optionally include intent filters that declare the capabilities of the component so it can respond to intents from other applications. 當你在你的應用的manifest中聲明一個組件時,你能夠可選擇包括intent filters(意圖過濾器),來指定組件的功能,以讓其能響應其餘應用的intents.

    You can declare an intent filter for your component by adding an <intent-filter> element as a child of the component's declaration element.

    你能夠加一個組件聲明的元素的子元素<intent-filter>,爲你組件聲明一個意圖過濾器,

    For example, an email application with an activity for composing a new email might declare an intent filter in its manifest entry to respond to "send" intents (in order to send email).

    好比,一個email應用中,新建emali的一個activity可能在它的manifest 中聲明瞭一個意圖過濾器,以便能響應」send」意圖(爲了發送郵件)

    An activity in your application can then create an intent with the 「send」 action (ACTION_SEND), which the system matches to the email application’s 「send」 activity and launches it when you invoke the intent with startActivity().而後,在你的應用中的一個activity,建立了一個帶有」send」 ACTION_SEND的意圖,.當你調用startActivity()方法,啓動該意圖過濾器時,系統將其匹配到email應用的「send」活動,並運行它。

    For more about creating intent filters, see the Intents and Intent Filters document. 關於建立意圖過濾器的詳細信息,參考Intents and Intent Filters 文檔

     
    Declaring application requirements聲明應用請求

    There are a variety of devices powered by Android and not all of them provide the same features and capabilities. 有許多設備裝了Android,但它們並不提供全部相同的特色和功能.

    In order to prevent your application from being installed on devices that lack features needed by your application, 爲了不你的應用,裝在一個沒有你應用所必特徵的設備上.

    it's important that you clearly define a profile for the types of devices your application supports by declaring device and software requirements in your manifest file.

    經過在你的manifest文件中聲明軟件硬件要求,明瞭的指出你的應用支持的硬件類型是很是重要的

    Most of these declarations are informational only and the system does not read them, but external services such as Android Market do read them in order to provide filtering for users when they search for applications from their device.大多數聲明僅僅只是信息,系統並不讀取他們,但像Android市場這樣的其餘服務,將讀取它們,以便讓用戶在爲他們的設備尋找應用時,能夠進行篩選.

    For example, if your application requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file.

    好比,若是你的應用須要有照相機,而且使用的API是2.1(API Level 7),你應在你r的manifest文件中聲明這些要求.

    That way, devices that do not have a camera and have an Android version lower than 2.1 cannot install your application from Android Market.

    這樣,那些沒有照相機而且Android版本低於2.1的設備,就不能從Android市場上安裝你的應用.

    However, you can also declare that your applicaiton uses the camera, but does not require it. 但,你也能夠聲明你的應使用camera,但沒必要需要求

    In that case, your application must perform a check at runtime to determine if the device has a camera and disable any features that use the camera if one is not available.

    那種狀況,你的應用必在運行時一個檢查,以肯定設備是否有一個照相機,若是沒有照相機,並禁止與照相相關的功能。

    Here are some of the important device characteristics that you should consider as you design and develop your application: 下面是一些重要的設備特性,你在設計和開發應用時必需要考慮的..

    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). 爲了能從它們的屏幕尺寸來分類設備,Android爲每一個設備定義了兩個特性:屏幕尺寸(屏幕的物理尺寸)和解釋度(在屏上的像素的物理密度,或者dpi--每英寸的點數)
     
    To simplify all the different types of screen configurations, the Android system generalizes them into select groups that make them easier to target.
    爲了簡化屏幕配置的全部不一樣類型,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 p_w_picpath resources. 黑認狀況下,你的應用是兼容全部屏幕尺寸和解釋度的,由於Android系統對此作了適當的調整,以使得它適合你的UI佈局和圖像資源

    However, you should create specialized layouts for certain screen sizes and provide specialized p_w_picpaths for certain densities, using alternative layout resources, and by declaring in your manifest exactly which screen sizes your application supports with the <supports-screens> element.

    然而,你應爲某個屏幕尺寸建立特殊的佈局,併爲某些解釋度提供特定的圖像,使用可選的資源,並在你的manifest文件中用<supports-screens> 元素聲明,以明確指出你的應用支持的屏幕尺寸.

     

    For more information, see the Supporting Multiple Screens document. 更多信息,參考Supporting Multiple Screens文檔

     

    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. 許多設備爲用提供了一個不一樣類型輸入裝置,好比,硬件鍵盤,軌跡球,five-way導航pad.
    If your application requires a particular kind of input hardware, then you should declare it in your manifest with the <uses-configuration> element.若是你的應用必需要一個特別的輸入硬件,那麼你應在你的應用中使用 <uses-configuration>元素聲明However, it is rare that an application should require a certain input 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. 在一個裝有Android的設備中,有許多軟硬件特性,有可能有,或有可能沒有。好比照相機,光敏器件,藍牙,或某個版本的OpenGL,或者觸模屏的精度.
    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的設備中某個特色是可用的(除了標準的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.
    不一樣的Android設備,常常運行不一樣的Android平臺版本,好比Android1.6或者2.3.
    Each successive version often includes additional APIs not available in the previous version. 每個成功的版本一般包括在前一個版本中不可用的API。
    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).
    爲了指出,那些APIs集是可用的,每一個平臺版本指定了一個 API Level(好比, 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. 若是你使用的APIs是在1.0版以後,加入到平臺的,你應該用 <uses-sdk> 元素,聲明最小API級別,這樣就指出了那些API將被採用.
    It's important that you declare all such requirements for your application, 爲你的應用聲明全部必要性的要求很是重要
    because, when you distribute your application on Android Market, 由於,當你把你的應用發佈到Android市場
    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. 一樣,你的應用應該只能在知足全部你應用需求的設備上纔可用
  • For more information about how Android Market filters applications based on these (and other) requirements, see the Market Filters document.

  • 更多關於Android市場如何基於這些需求過濾的,請看Market Filters文檔

    Application Resources應用資源

    An Android application is composed of more than just code—it requires resources that are separate from the source code, such as p_w_picpaths, audio files, and anything relating to the visual presentation of the application. 一個Android應用的組成不只只是代碼----它還有與代碼獨立的資源,好比圖像,音頻文件,及與應用可顯圖像任何其餘相關的(東東)

  • For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files.

  • 好比,你應該定義動畫,菜單,風格,顏色,和用XML文件定義活動的佈局

  • Using application resources makes it easy to update various characteristics of your application without modifying code and—by providing sets of alternative resources—enables you to optimize your application for a variety of device configurations (such as different languages and screen sizes). 使用應用資源,能讓你的應用在不修改任何代碼的狀況下容易的升級各類特性---而且經過提供一套可選取的資源--能優化你的應用在各類配置不一樣的設備中的表現(好比不一樣的語言和屏幕尺寸)

  • For every resource that you include in your Android project, the SDK build tools define a unique integer ID, which you can use to reference the resource from your application code or from other resources defined in XML.

  • 對於每一個包含在你的Android工程中的資源,SDK將其定義成一個惟一的整型ID,這樣你就能夠在你的代碼中或在XML文件中定義的其餘資源中引用它For example,

  • if your application contains an p_w_picpath 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 p_w_picpath and insert it in your user interface. 若是你的應用包括一個圖片名字是logo.png(保存在res/drawable/directory),SDK工具將生成一個資源ID命名成R.drawable.logo,你能夠用它來引用圖片,並插入你的用戶界面中

  • One of the most important aspects of providing resources separate from your source code is the ability for you to provide alternative resources for different device configurations. 提供與你的代碼分開的資源的一個很重要的方面是,使得你能爲不一樣的配置的設備提供可選資源.

  • For example, by defining UI strings in XML, you can translate the strings into other languages and save those strings in separate files.

  • 好比,在XML中定義UI字串,你能夠把字串翻譯成各類不一樣的語言並保存在不一樣的文件中

  • Then, based on a language qualifier that you append to the resource directory's name (such as res/values-fr/ for French string values) and the user's language setting, the Android system applies the appropriate language strings to your UI. 而後,以基於語言限定詞,你能夠追加資源目錄名(好比res/values-fr/ 用語法語資源),和用戶語言設置,Android系會將相應的資源應用到你的UI中.

    Android supports many different qualifiers for your alternative resources. Android爲你的可選資源,支持許多不一樣的qualifiers (限定詞)

  • The qualifier is a short string that you include in the name of your resource directories in order to define the device configuration for which those resources should be used.

  • 限定詞是一個包括在你的目錄名中的一個簡短的字串,是爲了定義那些資源將用在,該配置的設備上.

  • As another example, you should often create different layouts for your activities, depending on the device's screen orientation and size. 再如,因爲設備的屏幕的方向和尺寸不一樣,你一般須要爲你的活動定義不一樣的佈局.

  • For example, when the device screen is in portrait orientation (tall), you might want a layout with buttons to be vertical, but when the screen is in landscape orientation (wide), the buttons should be aligned horizontally. 好比,若設備的屏幕是豎向(高),你可能要一個帶有重直button 的佈局,當屏幕是橫向的(寬),按鈕應是水平對齊的

  • To change the layout depending on the orientation, you can define two different layouts and apply the appropriate qualifier to each layout's directory name.

  • 要根據方向來改變佈局,你要定義兩個不一樣的佈局,並在佈局的目錄名中使用相應的限定詞(qualifier)

  • Then, the system automatically applies the appropriate layout depending on the current device orientation. 而後,系統將自動根據當前的設備朝向來應用相應的佈局

    For more about the different kinds of resources you can include in your application and how to create alternative resources for various device configurations, see the Application Resources developer guide. 要詳細瞭解,你的應用中能包含的各類資源,及如何爲各類配置的設備建立可選資源,請看Application Resources開發指南

    <終於翻譯完了,花了一天多的時間>

相關文章
相關標籤/搜索