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:一旦安裝到了一個設備,每一個應用生存在它本身的安全沙箱中數據庫
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
然而,應用能夠有多種方法來與其餘應用,共享數據及訪問系統服務框架
That covers the basics regarding how an Android application exists within the system. 上述了一個應用怎樣存在於一個系統中的相關基本概念,
The rest of this document introduces you to:這個文檔的其餘部分將向你介紹
Application components are the essential building blocks of an Android application. 應用組件是構建Android應塊的關鍵。
Each component is a different point through which the system can enter your application.每一個組件是一個不一樣的入點,系統能夠從這些點進入到你的應用。
Not all components are actual entry points for the user and some depend on each other, 對於用戶來講,並非每一個組件都是實際的入點,但它們之間有一些依賴
but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your application's overall behavior.可是每個存在的組件都有它本身的一個入點,並扮演一個特定的角色--每個都是獨一無二的構建塊,幫助你定義你的應用的總體行爲
There are four different types of application components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.有四個不一樣類型的應用組件,每一個類型服務於一個不一樣的目的,並有不一樣的生命週期,生命週期定義瞭如何建立和銷燬它.
Here are the four types of application components:下面是四種應用組件
An activity is implemented as a subclass of Activity
and you can learn more about it in the Activities developer guide.一個activity被看成Activity
的子類來實現的,在Activities開發指南中,你能夠學到更多關於它的使用
A service is implemented as a subclass of Service
and you can learn more about it in the Services developer guide.一個服務是做爲Service
子類來實現的,在Services開發南中你能學到更多關於它的使用
ContactsContract.Data
) to read and write information about a particular person. 所以,任何捅用適當權限的應用,能夠查詢內容提供者的部分來(好比
ContactsContract.Data
)讀取和寫入關於某我的的信息
A content provider is implemented as a subclass of ContentProvider
and must implement a standard set of APIs that enable other applications to perform transactions.
一個內容提供者被看成的子類實現,而且必須實現一套標準的APIs,以讓其餘的應用能執行交換操做。
For more information, see the Content Providers developer guide.參考Content Providers開發指南,以瞭解更多信息.
A broadcast receiver is implemented as a subclass of BroadcastReceiver
and each broadcast is delivered as an Intent
object. For more information, see the BroadcastReceiver
class.
一個廣播接收者,是看成BroadcastReceiver
子類被實現的.每一個廣播接收者都是從Intent
對象衍生出來的。更多信息,請參考BroadcastReceiver
類
A unique aspect of the Android system design is that any application can start another application’s component.
任何一個應用能啓動另外一個其餘應用的組件,是Android系統設計獨一無二的方面(aspect).
For example, if you want the user to capture a photo with the device camera, 好比,你想要用設備的照相機拍一張圖片
there's probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself. 其餘的應用已經有了這個功能,而且你的應用可使用它,而不須要你本身去開發一個拍照相的activity.
You don't need to incorporate or even link to the code from the camera application. Instead, you can simply start the activity in the camera application that captures a photo.
你並不須要合併(包含)或者甚至是連接camera應中的代碼; 而只是,簡單的啓動camera應用中的活動,來拍照就能夠了.
When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application
當拍照完成,甚至把照片返回給你的應用,因此你能使用它。對於用戶來說,camera像是你應用中一部分
When the system starts a component, it starts the process for that application (if it's not already running) and instantiates the classes needed for the component.
當系統開啓一個組件時,它會啓動那個應用的進程(若是該應用沒有運行),並實例化該組件所須要的類.
For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process.
舉例,若是你的應用開啓一個camera應用的activity,來拍照,這個activity將運行在屬於camera應用的進程中,而不是在你的應用的進程中
Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main()
function, for example).
所以,不像大多數其餘的系統的應用,Android應用,沒有單個的入點(好比沒有main()函數)
Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component from another application.
由於系統運行的每一個應用,在一個帶有文件權限的,獨立的進程中,這樣限制了對其餘應用的訪問,你的應用不能直接訪問其餘應用中的組件
The Android system, however, can. So, to activate a component in another application, you must deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you.
但時,Android系統也能激活其餘應用的組件.你必須傳一個消息給系統,指定你想要啓動的組件,而後系統爲你激活這個組件
Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent.
4個組件中的其中三個組件---activities,serivces,和broadcast receivers----是被叫作intent的異步消息激活的.
Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another.在運行時,Intents把某個的組件與其餘的組件互相邦定,而無論這個組件是否屬於你的應用仍是其餘的應用(你能夠把它們想像成一個消息,用於請求一個其餘組件的動做)
An intent is created with an Intent
object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.一個intent是一個由Intent建立的對象.該對象定義了一個激活某個特定組件或者某個組件類型的消息,一個intent能夠是顯示的,一樣,也能夠是隱式的.
For activities and services, an intent defines the action to perform (for example, to "view" or "send" something) and may specify the URI of the data to act on (among other things that the component being started might need to know).
對於activities和services,一個intent(意圖)定義了一個要執行的動做(好比:to」view」或"send" 些什麼),並指定了要採用的URI格式的數據(其中一些,是其餘組件啓動所須要知道的)
For example, an intent might convey a request for an activity to show an 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:下面是激活各類類型組件的幾個方法
Intent
to startActivity()
or startActivityForResult()
(when you want the activity to return a result). 你能夠經過傳一個(或者一些要作新的事情)Intent
參數給startActivity()
或startActivityForResult()
(當你想要activity返回一個參數)函數(),來啓動一個activity.Intent
to startService()
. Or you can bind to the service by passing an Intent
to bindService()
. 你能夠傳一個Intent
給startService()
方法,(或給一個新的指令給正在運行的服啓),或者你能夠傳一個Intent
給bindService()
方法來邦定到服務.Intent
to methods like sendBroadcast()
, sendOrderedBroadcast()
, or sendStickyBroadcast()
.query()
on a ContentResolver
. 你能夠對ContentResolver
調用query()
方法,對內容提供者進行查詢For more information about using intents, see the Intents and Intent Filters document.關於使用intents的詳細信息,請看ntents and Intent Filters 文檔
More information about activating specific components is also provided in the following documents: Activities, Services, BroadcastReceiver
and Content Providers.
在後面的文檔中,也有一些關於激活某個組件的信息Activities, Services, BroadcastReceiver
and Content Providers.
Before the Android system can start an application component, the system must know that the component exists by reading the application's AndroidManifest.xml
file (the "manifest" file). 在Android系統開啓一個應用組件以前,系統必須經過讀取AndroidManifest.xml文件來知道組件的存在
Your application must declare all its components in this file, which must be at the root of the application project directory.
你的應用必須把它全部的組件聲明在這個文件中,而且必須在應用工程的根目錄下
The manifest does a number of things in addition to declaring the application's components, such as:
這個manifest文件除了聲明組件外,還處理了許多其餘的事情,好比
The primary task of the manifest is to inform the system about the application's components. For example, a manifest file can declare an activity as follows:
manifest文件的主要任務是告訴系統,應用的組件,好比,一個manifest能夠這樣聲明一個activity
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:icon="@drawable/app_icon.png" ... >
<activity android:name="com.example.project.ExampleActivity"
android:label="@string/example_label" ... >
</activity>
...
</application>
</manifest>
In the <application>
element, the android:icon
attribute points to resources for an icon that identifies the application.在<application>
元素中,android:icon
指定應用的icon資源
In the <activity>
element, the android:name
attribute specifies the fully qualified class name of the Activity
subclass and the android:label
attributes specifies a string to use as the user-visible label for the activity.
在<activity>
元素中的,android:name
屬性,指定Activity
子類的徹底類名
You must declare all application components this way:你必須這樣聲明 全部應用的組件
<activity>
elements for activities 聲明活動的元素<service>
elements for services 聲明服務的元素<receiver>
elements for broadcast receivers 聲明廣播接收者元素<provider>
elements for content providers 聲明內容提供者元素Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run.
在你代碼中包含的,Activites,services和內容提供者,若沒有在manifest中聲明,對系統來講是不可見的,即將永遠不會運行
However, broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver
objects) and registered with the system by calling registerReceiver()
.可是,廣播接收者便可以在manifest中聲明,也能夠在代碼中動態建立(作爲BroadcastReceiver
對象)而且經過registerReceiver()
方法向系統註冊
For more about how to structure the manifest file for your application, see the The AndroidManifest.xml File documentation. 瞭解manifest文件的詳細構建過程,請看The AndroidManifest.xml File文檔
As discussed above, in Activating Components, you can use an Intent
to start activities, services, and broadcast receivers.
就如在上面的Activating Components中所討論的,你能夠用一個Activating Components啓動activities,services和broadcast 接收者.
You can do so by explicitly naming the target component (using the component class name) in the intent. 你也能夠在intent中顯式的指定目標組件(使用組件類名)
However, the real power of intents lies in the concept of intent actions. 然而,intent真正強大的是它的intent action.(動做)
With intent actions, you simply describe the type of action you want to perform (and optionally, the data upon which you’d like to perform the action) and allow the system to find a component on the device that can perform the action and start it.
經過使用intent動做,你只須簡單的描述你要執行的action類型,(而且,可選的與執行動做有關的數據),而且容許系統在設備上找到一個組件,這樣就能夠執行那個動做並啓動它
If there are multiple components that can perform the action described by the intent, then the user selects which one to use.
若是有多個組件能夠執行,intent指定的action,那麼用戶選擇執行那一個.
The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other applications on the device.
經過比較設備上的其餘應用的manifest文件上的intent filters與接收到的intent.系統肯定那個組件能夠響應一個intent.
When you declare a component in your application's manifest, you can optionally include intent filters that declare the capabilities of the component so it can respond to intents from other applications. 當你在你的應用的manifest中聲明一個組件時,你能夠可選擇包括intent filters(意圖過濾器),來指定組件的功能,以讓其能響應其餘應用的intents.
You can declare an intent filter for your component by adding an <intent-filter>
element as a child of the component's declaration element.
你能夠加一個組件聲明的元素的子元素<intent-filter>
,爲你組件聲明一個意圖過濾器,
For example, an email application with an activity for composing a new email might declare an intent filter in its manifest entry to respond to "send" intents (in order to send email).
好比,一個email應用中,新建emali的一個activity可能在它的manifest 中聲明瞭一個意圖過濾器,以便能響應」send」意圖(爲了發送郵件)
An activity in your application can then create an intent with the 「send」 action (ACTION_SEND
), which the system matches to the email application’s 「send」 activity and launches it when you invoke the intent with startActivity()
.而後,在你的應用中的一個activity,建立了一個帶有」send」 ACTION_SEND
的意圖,.當你調用startActivity()
方法,啓動該意圖過濾器時,系統將其匹配到email應用的「send」活動,並運行它。
For more about creating intent filters, see the Intents and Intent Filters document. 關於建立意圖過濾器的詳細信息,參考Intents and Intent Filters 文檔
There are a variety of devices powered by Android and not all of them provide the same features and capabilities. 有許多設備裝了Android,但它們並不提供全部相同的特色和功能.
In order to prevent your application from being installed on devices that lack features needed by your application, 爲了不你的應用,裝在一個沒有你應用所必特徵的設備上.
it's important that you clearly define a profile for the types of devices your application supports by declaring device and software requirements in your manifest file.
經過在你的manifest文件中聲明軟件硬件要求,明瞭的指出你的應用支持的硬件類型是很是重要的
Most of these declarations are informational only and the system does not read them, but external services such as Android Market do read them in order to provide filtering for users when they search for applications from their device.大多數聲明僅僅只是信息,系統並不讀取他們,但像Android市場這樣的其餘服務,將讀取它們,以便讓用戶在爲他們的設備尋找應用時,能夠進行篩選.
For example, if your application requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file.
好比,若是你的應用須要有照相機,而且使用的API是2.1(API Level 7),你應在你r的manifest文件中聲明這些要求.
That way, devices that do not have a camera and have an Android version lower than 2.1 cannot install your application from Android Market.
這樣,那些沒有照相機而且Android版本低於2.1的設備,就不能從Android市場上安裝你的應用.
However, you can also declare that your applicaiton uses the camera, but does not require it. 但,你也能夠聲明你的應使用camera,但沒必要需要求
In that case, your application must perform a check at runtime to determine if the device has a camera and disable any features that use the camera if one is not available.
那種狀況,你的應用必在運行時一個檢查,以肯定設備是否有一個照相機,若是沒有照相機,並禁止與照相相關的功能。
Here are some of the important device characteristics that you should consider as you design and develop your application: 下面是一些重要的設備特性,你在設計和開發應用時必需要考慮的..
The screen sizes are: small, normal, large, and extra large.屏幕大小:小,正常,大和極大
The screen densities are: low density, medium density, high density, and extra high density. 屏幕解釋度:低解釋度,中解釋度,高解釋度,和極高解釋度
By default, your application is compatible with all screen sizes and densities, because the Android system makes the appropriate adjustments to your UI layout and 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文檔
<uses-configuration>
element.若是你的應用必需要一個特別的輸入硬件,那麼你應在你的應用中使用
<uses-configuration>
元素聲明However, it is rare that an application should require a certain input configuration. 但時,應用必需要一個特別的輸入配置的狀況是極少的.
<uses-feature>
element.
<uses-feature>
元素聲明你的應用支持的特徵.
<uses-sdk>
element. 若是你使用的APIs是在1.0版以後,加入到平臺的,你應該用
<uses-sdk>
元素,聲明最小API級別,這樣就指出了那些API將被採用.
For more information about how Android Market filters applications based on these (and other) requirements, see the Market Filters document.
更多關於Android市場如何基於這些需求過濾的,請看Market Filters文檔
An Android application is composed of more than just code—it requires resources that are separate from the source code, such as 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開發指南
<終於翻譯完了,花了一天多的時間>