【正文】
因此,不像其它大多數(shù)系統(tǒng)上的應(yīng)用程序,Android應(yīng)用程序并沒(méi)有為應(yīng)用程序提供一個(gè)單獨(dú)的入口點(diǎn)(比如說(shuō),沒(méi)有main()函數(shù)),而是為系統(tǒng)提供了可以實(shí)例化和運(yùn)行所需的必備組件。它只是在有需求的時(shí)候啟動(dòng)了其它應(yīng)用程序的那個(gè)功能部分。例如,如果你的應(yīng)用程序需要顯示一個(gè)圖片卷動(dòng)列表,而另一個(gè)應(yīng)用程序已經(jīng)開發(fā)了一個(gè)合用的而又允許別的應(yīng)用程序使用的話,你可以直接調(diào)用那個(gè)卷動(dòng)列表來(lái)完成工作,而不用自己再開發(fā)一個(gè)。 3 默認(rèn)情況下,每個(gè)應(yīng)用程序均被賦予一個(gè)唯一的Linux用戶ID,并加以權(quán)限設(shè)置,使得應(yīng)用程序的文件僅對(duì)此用戶及此應(yīng)用程序可見——盡管也有其它的方法使得這些文件同樣能為其他應(yīng)用程序所訪問(wèn)。從多個(gè)角度來(lái)看,每個(gè)Android應(yīng)用程序都存在于它自己的世界之中: 1 默認(rèn)情況下,每個(gè)應(yīng)用程序均運(yùn)行于它自己的Linux進(jìn)程中。AAPT工具把編譯后的Java代碼連同應(yīng)用程序所需的其它數(shù)據(jù)和資源文件一起打包到一個(gè)Android包文件中。t have any filters, it can be activated only by intents that explicitly name the ponent as the target.For a broadcast receiver that39。re declared in the manifest file. Here39。s ponents. For example, an activity might be declared as follows:The name attribute of the activityelement names the Activity subclass that implements the activity. The icon and label attributes point to resource files containing an icon and label that can be displayed to users to represent the activity.The other ponents are declared in a similar way — serviceelements for services, receiverelements for broadcast receivers, and providerelements for content providers. Activities, services, and content providers that are not declared in the manifest are not visible to the system and are consequently never run. However, broadcast receivers can either be declared in the manifest, or they can be created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling ().For more on how to structure a manifest file for your application, see The Android File.Intent filtersAn Intent object can explicitly name a target ponent. If it does, Android finds that ponent (based on the declarations in the manifest file) and activates it. But if a target is not explicitly named, Android must locate the best ponent to respond to the intent. It does so by paring the Intent object to the intent filters of potential targets. A ponent39。s code, files, and resources.The manifest is a structured XML file and is always named for all applications. It does a number of things in addition to declaring the application39。re in a longrunning conversation with the user and may remain active, even when idle, as long as the conversation continues. Similarly, services may also remain running for a long time. So Android has methods to shut down activities and services in an orderly way:1. An activity can be shut down by calling its finish() method. One activity can shut down another activity (one it started with startActivityForResult()) by calling finishActivity().2. A service can be stopped by calling its stopSelf() method, or by calling ().Components might also be shut down by the system when they are no longer being used or when Android must reclaim memory for more active ponents. A later section, Component Lifecycles, discusses this possibility and its ramifications in more detail.The manifest fileBefore Android can start an application ponent, it must learn that the ponent exists. Therefore, applications declare their ponents in a manifest file that39。s responding to a broadcast message. So there39。s onStart() method and passes it the Intent object. Similarly, an intent can be passed to () to establish an ongoing connection between the calling ponent and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.A later section, Remote procedure calls, has more details about binding to a service.3. An application can initiate a broadcast by passing an Intent object to methods like (), (), and () in any of their variations.Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods. For more on intent messages, see the separate article, Intents and Intent Filters.Shutting down ponentsA content provider is active only while it39。s passed to the calling activity39。s onNewIntent() method to pass it any subsequent intents. One activity often starts the next one. If it expects a result back from the activity it39。s a request that should be handled by a particular ponent, Android makes sure that the application process of the ponent is running, starting it if necessary, and that an appropriate instance of the ponent is available, creating the instance if necessary.Activating ponents: intentsContent providers are activated when they39。 it cooperates with the provider to manage any interprocess munication that39。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.Content providersA 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 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 network 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