Tuesday, March 29, 2011

Android Application Fundamentals--Android四大組成區塊介紹

原文出處於此

以下中文是用我自己破爛的英文翻譯,若有翻譯錯誤的地方請多包涵,也歡迎指正。

Application Components---
Application components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your application's overall behavior.
Android的應用程式並不是需要以下的每個區塊才能執行,但是每一個區塊的存在都扮演著它們特殊的角色,並且幫助定義應用程式能夠從頭到完成整個程序。


There are four different types of application components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.
以下有四種不同型態的應用程式元件。每種形態提供了截然不同的目的,並且定義了每個元件在被建立以及被摧毀時擁有完全不相同的生命週期。

Here are the four types of application components:

Activities
An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.
一個Activity通常會有一個畫面(single screen)呈現在使用者介面上。舉例來說,一個郵件應用程式可能擁有一個activity去呈現一個新郵件清單,另一個activity用來呈現輸入新郵件的內容,再由另一個Activity去讀取郵件內容。然而,多個activity是一起工作於一個使用者所使用的郵件應用程式,且每個activity是互相依賴的。同樣地,另一個不同的應用程式可以開始於任何一個activity(只要這個郵件應用程式允許的話!)。舉例來說,一個相機應用程式可以使用郵件應用程式的activity去撰寫新郵件而分享照片給其他使用者。

An activity is implemented as a subclass of Activity and you can learn more about it in the Activities developer guide.
一個activity實作了Activity類別,這部份容我之後再介紹,或自行參閱Android developer guide。

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的互動。

A service is implemented as a subclass of Service and you can learn more about it in the Services 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提供的聯絡人資訊,並且去讀取和覆寫特定聯絡人的資訊。

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去儲存notes。

A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other applications to perform transactions. For more information, see the Content Providers developer guide.

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沒有顯示使用者介面,但是當一個需要被廣播的事件發生時,創造了一個通知狀態列去警示使用者。一般來說,broadcast receiver只是一個其他元件的橋樑,在整個工作中做極微量的事情。當BroadcastReceiver被觸發時系統會依需求來執行相對應的程式。

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.

A unique aspect of the Android system design is that any application can start another application’s component. For example, if you want the user to capture a photo with the device camera, there's probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself. You don't need to incorporate or even link to the code from the camera application. Instead, you can simply start the activity in the camera application that captures a photo. When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application.
Android系統設計有一個很特殊的方面是任何應用程式能夠啟動其他應用程式的元件。如果你想要使用者使用裝置拍照,或許有其他的應用程式能夠代替使用於你拍完照片之後,你沒有需要去整合甚至是連結相機應用程式。你能夠簡單的啟動一個相機應用程式去拍照,當動作完成時,你能夠在應用程式中使用你剛剛所拍下的照片。對於使用者來說,這看起來彷若相機是你的應用程式的一部分。

When the system starts a component, it starts the process for that application (if it's not already running) and instantiates the classes needed for the component. For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process. Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main() function, for example).
當系統開啟了一個元件,元件啟動了該應用程式中的程序(如果它還沒有開始執行的話),並且會立即使用該元件所需的類別(classes)。舉例來說,如果你使用的某個應用程式啟動了相機應用程式中的某個activity來拍照,該activity會在隸屬於相機應用程式中的程序上執行,而不是在你使用的應用程式中。因此不同於其他系統的應用程式,Android應用程式並沒有一個單純的進入點(entry point)(這裡指的是沒有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系統上是可以做到的 (好大一個瓜啊XDDD)。所以若需觸發其他應用程式的元件,你必須向系統傳遞一個訊息,代表著你有意圖(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的非同步訊息所觸發。intent在執行期間結合了各個元件,不論這個元件是屬於你的應用程式還是其他應用程式。

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.

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).
intent定義了activities和services動作的履行(例如:"觀看"或"寄"某物)並且明確的指出資料會建立於哪個URI上,例如一個intent可能會向一個activity傳達請求去打開一個網頁連結。在某些情況下,你可以啟動一個activity去接收一個結果,在相同的情況下,activity也會經由一個intent去回傳此結果。

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

The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver. The content resolver handles all direct transactions with the content provider so that the component that's performing transactions with the provider doesn't need to and instead calls methods on the ContentResolver object. This leaves a layer of abstraction between the content provider and the component requesting information (for security).
Content provider是一種不被intent所啟動的元件,但是它會被啟動於一個來自content resolver的目標請求。content resolver處理了所有經由content provider的直接交易,以致於元件可以利用provider完成交易,不需要呼叫ContentResolver物件的方法。

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 to startActivity() or startActivityForResult() (when you want the activity to return a result).
你能夠經由intent使用startActivity() or startActivityForResult()方法去起始一個activity。
.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().
你可以經由intent使用startService()方法去起始一個service,或者使用bindService()去結合其他的service。
.You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
你可以經由intent使用sendBroadcast()、sendOrderedBroadcast()、sendStickyBroadcast()等方法啟動一個廣播。
.You can perform a query to a content provider by calling query() on a ContentResolver.
你能夠在ContentResolver上呼叫query()方法,並在content provider中完成一次的查詢動作。

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.
在系統啟動一個應用程式元件之前,系統必須經由讀取應用程式的設定檔:AndroidManifest.xml檔案來了解有哪些元件的存在。應用程式所需的元件必須明確的定義在此檔案中,並且指出它們所擁有的能力及需求。

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

.Identify any user permissions the application requires, such as Internet access or read-access to the user's contacts.
.Declare the minimum API Level required by the application, based on which APIs the application uses.
.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.
.And more

Application Resources

An Android application is composed of more than just code—it requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the application. For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files. Using application resources makes it easy to update various characteristics of your application without modifying code and—by providing sets of alternative resources—enables you to optimize your application for a variety of device configurations (such as different languages and screen sizes).
一個Android應用程式不光只是由語法組成,它還包含了影像、音訊檔案還有其他任何有關視覺傳達的檔案,而你應該於xml檔案中去定義這些選單、風格、顏色、使用者介面的版面配置。使用應用程式資源,將會更容易去更新你軟體中各種不同的特徵而不用更改任何的程式碼,並且在可選擇資源的條件下,准許你的應用程式能夠為了多樣性的裝置型態而更加的完美(例如不同的語言和螢幕大小)。

For every resource that you include in your Android project, the SDK build tools define a unique integer ID, which you can use to reference the resource from your application code or from other resources defined in XML. For example, if your application contains an image file named logo.png (saved in the res/drawable/ directory), the SDK tools generate a resource ID named R.drawable.logo, which you can use to reference the image and insert it in your user interface.
對於每一個包含於Android專案的資源,SDK建置工具會將其定義一個特殊的ID,讓你可以使用該資源。舉例來說,如果你的應用程式包含了一個名為logo.png的檔案,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. 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定義使用者介面所需的字串時,你可以將字串翻譯成其他語言儲存在其他檔案中,並根據該語言的特性附加檔案夾(例如可將法語版本的字串檔案存在 res/values-fr/ 檔案夾中)。根據使用者的語言設定,Android系統會應用適當的語言版本於使用者介面。

Android supports many different qualifiers for your alternative resources. The qualifier is a short string that you include in the name of your resource directories in order to define the device configuration for which those resources should be used. As another example, you should often create different layouts for your activities, depending on the device's screen orientation and size. For example, when the device screen is in portrait orientation (tall), you might want a layout with buttons to be vertical, but when the screen is in landscape orientation (wide), the buttons should be aligned horizontally. To change the layout depending on the orientation, you can define two different layouts and apply the appropriate qualifier to each layout's directory name. Then, the system automatically applies the appropriate layout depending on the current device orientation.
Android支援了非常多不同的格式在可選擇的資源上。你也可以為了你的activity創造不同形式的版面(layout),取決於螢幕大小和方位。為了改變不同的版面配置去因應螢幕方向的不同,你可以定義兩個不同的版面,讓系統能因為螢幕方向的不同而給予適當的版面配置。

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.

0 comments:

Post a Comment