【正文】
n 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. Content providers A content provider makes a specific set of the application39。s not already running). While connected, you can municate with the service through an interface that the service exposes. For the music service, this interface 9 might allow users to pause, rewind, stop, and restart the playback. Like activities and the other ponents, services run in the main thread of the application process. So that they won39。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 work or calculate something and provide the result to activities that need it. Each service extends the Service base class. A prime example is a media player playing songs from a play list. The player application would probably have one or more activities that allow the user to choose songs and start playing them. However, the music playback itself would not be handled by an activity because users will expect the music to keep playing even after they leave the player and begin something different. To keep the music going, the media player activity could start a service to run in the background. The system would then keep the music playback service running even after the activity that started it leaves the screen. It39。s interaction with the user takes place. For example, a view might display a small image and initiate an action when the user taps that image. Android has a number of readymade views that you can use — including buttons, text fields, scroll bars, menu items, check boxes, and more. A view hierarchy is placed within an activity39。t incorporate the code of the other application or link to it. Rather, it simply starts up that piece of the other application when the need arises. For this to work, the system must be able to start an application process when any part of it is needed, and instantiate the Java objects for that part. Therefore, unlike applications on most other systems, Android applications don39。s possible to arrange for two applications to share the same user ID, in which case they will be able to see each other39。s no longer needed and system resources are required by other applications. 2. Each process has its own virtual machine (VM), so application code runs in isolation from the code of all other applications. 3. By default, each application is assigned a unique Linux user ID. Permissions are set so that the application39。s the file users download to their devices. All the code in a single .apk file is considered to be one application. In many ways, each Android application lives in its own world: 1. By default, every application runs in its own Linux process. Android starts the process when any of the application39。 7 Application Fundamentals Android applications are written in the Java programming language. The piled Java code — along with any data and resource files required by the application — is bundled by the aapt tool into an Android package, an archive file marked by an .apk suffix. This file is the vehicle for distributing the application and installing it on mobile devices。 對于 在代碼中創(chuàng)建并注冊的 broadcast receiver來說, intent過濾器將被直接實例化IntentFilter為對象。返里有一個對先前例子的擴展,其中加入了針對 activity的兩個 intent過濾器: 一個組件可以擁有任意數(shù)量的 intent過濾器,每個都聲明了一套不同的功能。組件的 intent過濾器會通知 Android它所能處理的 intent類型。但如果 intent沒有顯式地指定一個目標,Android就必須找到最合適的組件來響應此 intent。 Intent過濾器 一個 Intent對象可以顯式地指定一個目標組件。未在manifest文件中進 行聲明的 activity、 service以及 content provider將不為系統(tǒng)所見,從而也就永不 會被運行。 icon和 label屬性指向包含展示給用戶的此 activity的圖標和標簽的資源文件。除了聲明此應用程序各個組件,它 會做很多其他工作,比如指明應用程序所需鏈接到的庫的名稱(除了默認 的 Android庫乀外)以及標出應用程序期望獲得的各種權限。因此,應用程序會在一個被打包到 Android包 中的 manifest文件中聲明它的組件, .apk文件還 將涵括應用程序的代碼、文件以及其它資源。稍后的組件的生命周期一節(jié),將對返種可能性及結果進 行更詳細的認論。 service可以通過調用自身的 stopSelf()方 法,或 調用 ()來停止。 activity可以通過調用自身的 finish()方法來關閉。與 此相似, service也會在很長一段時間內保持運行。 而 activity則不同,它提供了用戶界面。而 broadcast receiver僅在響應一條廣播信息的時候處于活動 狀態(tài)。 Android會調用所有對此廣播有興趣的 broadcast receiver的 onReceive()方法,將此 intent傳遞給它們。 稍后的遠程方法調用一節(jié)有關于 如何綁定至一個 service的更多細節(jié)。例如,一個 activity可以建立一個不前述的