1. 程式人生 > >【原創】Android之應用程式的組成部分

【原創】Android之應用程式的組成部分

        Android架構裡定義了4種應用程式構件,使用者的Android應用程式都是由這4種構件組成,他們分別是Activity(活動)、Service(服務)、BroadcastReceiver(廣播接收器)和ContentProvider(內容提供器)。下面我們分別來看看他們都是做什麼的。

1 Activity(活動)

        首先我們來看看官方的解釋。

    • An activity presents a visual user interface for one focused endeavor the user can undertake. For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions. A text messaging application might have one activity that shows a list of contacts to send messages to, a second activity to write the message to the chosen contact, and other activities to review old messages or change settings. Though they work together to form a cohesive user interface, each activity is independent of the others. Each one is implemented as a subclass of the Activity base class.

        Android系統中的活動和桌面作業系統中獨立的應用程式類似。活動都是有一些可執行程式碼組成的,活動可以由使用者或作業系統在需要的時候啟動。活動可以和使用者進行互動,還可以通過查詢(queries)和意圖(Intent)向其他活動(Activity)或服務(Service)獲取資料和服務。使用者編寫的大部分可執行程式碼是以活動的形式存在的。活動通常要負責螢幕的顯示:每一個活動都有一個顯示介面。如果一個活動不再處於活動狀態,作業系統可以通過殺死它而提高手機記憶體的使用效率。

        一個應用就像一個傳統瀏覽器一樣,每個Activity就像瀏覽器中開啟的頁面一樣。在傳統瀏覽器中,任何時刻我們只能瀏覽一個頁面,Android應用也是一樣。在我們使用Android應用,我們只能看到當前的Activity。我們會在後面的文章中詳細講解Activity的相關知識。如有需求,先參看《

Activities在Android平臺的完整生態系統》。一個Activity是一個應用程式元件,提供一個螢幕,使用者可以用來互動為了完成某項任務,例如撥號、拍照、傳送email、看地圖。每一個activity被給予一個視窗,在上面可以繪製使用者介面。視窗通常充滿螢幕,但也可以小於螢幕而浮於其它視窗之上。

        一個應用程式通常由多個activities組成,他們通常是鬆耦合關係。通常,應用程式中有一個activity會被指定為"main"activity,即當第一次啟動應用程式的時候呈現給使用者的那個activity。一個activity可以啟動另一個 activity,用以完成不同的動作。一個activity啟動,前一個activity就停止了,但是系統會保留activity在一個棧上(“back stack”)。當一個新activity啟動,它被推送到棧頂,取得使用者焦點。Back Stack符合簡單“後進先出”的原則,所以,當用戶完成當前activity然後點選back按鈕,它被彈出棧(並且被摧毀),然後之前的 activity恢復。

2 Service(服務)

        官方解釋:

    • A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class.
    • It's possible to connect to (bind to) an ongoing service (and start the service if it's not already running). While connected, you can communicate with the service through an interface that the service exposes. For the music service, this interface might allow users to pause, rewind, stop, and restart the playback.
    • Like activities and the other components, services run in the main thread of the application process. So that they won't block other components or the user interface, they often spawn another thread for time-consuming tasks (like music playback). See Processes and Threads, later.

        Android系統中的服務與桌面系統或者伺服器中的後臺程式有些類似。服務同樣是有一些可執行程式碼構成,它們啟動後一直在後臺執行,直到手持裝置關機為止。服務通常不會顯示使用者介面。像Activity和其他元件一樣,服務執行在應用程序中的主執行緒中,所以服務不會阻塞其他元件或使用者介面。

        服務是執行在後臺的一段程式碼。它可以執行在它自己的程序,也可以執行在其他應用程式程序的上下文(context)裡面,這取決於自身的需要。其它的元件可以繫結到一個服務(Service)上面,通過遠端過程呼叫(RPC)來呼叫這個方法。例如媒體播放器的服務,當用戶退出媒體選擇使用者介面,仍然希望音樂依然可以繼續播放,這時就是由服務 (service)來保證當用戶介面關閉時音樂繼續播放的。它跟Activity的級別差不多,但是他不能自己執行,需要通過某一個Activity或者其他Context物件來呼叫, Context.startService() 和Context.bindService()。兩種啟動Service的方式有所不同。這裡要說明一下的是如果你在Service的onCreate或者onStart做一些很耗時間的事情,最好在Service裡啟動一個執行緒來完成,因為Service是跑在主執行緒中,會影響到你的UI操作或者阻塞主執行緒中的其他事情。

3 BroadcastReceiver(廣播接收器)

        官方解釋:

    • A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many broadcasts originate in system code — for example, announcements that the timezone has changed, that the battery is low, that a picture has been taken, or that the user changed a language preference. 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.
    • An application can have any number of broadcast receivers to respond to any announcements it considers important. All receivers extend the BroadcastReceiver base class.
    • Broadcast receivers do not display a user interface. However, they may start an activity in response to the information they receive, or they may use the NotificationManager to alert the user. Notifications can get the user's attention in various ways — flashing the backlight, vibrating the device, playing a sound, and so on. They typically place a persistent icon in the status bar, which users can open to get the message.

        廣播接收器用於處理應用程式發出的服務請求、系統級別的事件訊息。這些事件訊息可能是Android系統本身發出的(比如電池電量過低),也可能來自系統系統中正在執行的其他應用程式。一個活動或者服務通過實現意圖(Intent)接收器來向其他應用程式提供服務,意圖接收器就是一段可執行程式碼,負責顯影其他活動或者服務發出的資料或服務請求。想要傳送請求的活動首先需要建立一個意圖物件,然後將這個意圖物件提交給Android作業系統,最後由Android作業系統決定使用哪一個應用程式處理該意圖。

4 ContentProvider(內容提供器)

        官方解釋:

    • A content provider makes a specific set of the application's data available to other applications. The data can be stored in the file system, in an SQLite database, or in any other manner that makes sense. The content provider extends the ContentProvider base class to implement a standard set of methods that enable other applications to retrieve and store data of the type it controls. However, applications do not call these methods directly. Rather they use a ContentResolver object and call its methods instead. A ContentResolver can talk to any content provider; it cooperates with the provider to manage any interprocess communication that's involved.

        內容提供器用於向其他活動或者服務提供資料服務。內容提供器通過使用標準的URI(Uniform Resource Identifier,統一資源識別符號)介面為其他應用程式提供資料服務。資料的請求者甚至不需要知道服務的提供者是誰。例如,某個應用程式發出一個查詢聯絡人資訊的請求,請求格式如下:

Content://contacts/people

        Android作業系統就會查詢系統中註冊了這種URI格式的內容提供器,並將請求傳送給相應的應用程式(如果給程式沒有啟動,系統會自動將其啟動)。如果有多個應用程式都註冊了同樣的URI,那麼作業系統就會提示使用者選擇其中的一個。

        在一個Android應用程式中,可能不會同時用到所有的4種構件,但是對於一個優秀的應用程式,一定會積極使用系統提供的這些機制,而不是重新編寫某些功能火影編碼一些對其他應用程式的引用。通過URI和意圖機制,Android為使用者提供了一個非常靈活的操作環境。使用者可以方便地新增、刪除和替換應用程式,URI和意圖機制的鬆散耦合使得各種程式可以和諧地一起工作。