

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、<p> Application Fundamentals</p><p> Android applications are written in the Java programming language. The compiled Java code — along with any data and resource files required by the application — i
2、s 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; it's the file users downloa
3、d to their devices. All the code in a single .apk file is considered to be one application.</p><p> In many ways, each Android application lives in its own world:</p><p> 1. By default, every
4、application runs in its own Linux process. Android starts the process when any of the application's code needs to be executed, and shuts down the process when it's no longer needed and system resources are requir
5、ed by other applications.</p><p> 2. Each process has its own virtual machine (VM), so application code runs in isolation from the code of all other applications.</p><p> 3. By default, each a
6、pplication is assigned a unique Linux user ID. Permissions are set so that the application's files are visible only to that user and only to the application itself — although there are ways to export them to other ap
7、plications as well.</p><p> It's possible to arrange for two applications to share the same user ID, in which case they will be able to see each other's files. To conserve system resources, applicat
8、ions with the same ID can also arrange to run in the same Linux process, sharing the same VM.</p><p> Application Components</p><p> A central feature of Android is that one application can ma
9、ke use of elements of other applications (provided those applications permit it). For example, if your application needs to display a scrolling list of images and another application has developed a suitable scroller and
10、 made it available to others, you can call upon that scroller to do the work, rather than develop your own. Your application doesn't incorporate the code of the other application or link to it. Rather, it simply star
11、ts up</p><p> 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
12、other systems, Android applications don't have a single entry point for everything in the application (no main() function, for example). Rather, they have essential components that the system can instantiate and run
13、as needed. There are four types of components:</p><p> Activities</p><p> An activity presents a visual user interface for one focused endeavor the user can undertake. For example, an activity
14、 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,
15、 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</p><p> An application might consist of jus
16、t one activity or, like the text messaging application just mentioned, it may contain several. What the activities are, and how many there are depends, of course, on the application and its design. Typically, one of the
17、activities is marked as the first one that should be presented to the user when the application is launched. Moving from one activity to another is accomplished by having the current activity start the next one.</p>
18、;<p> Each activity is given a default window to draw in. Typically, the window fills the screen, but it might be smaller than the screen and float on top of other windows. An activity can also make use of additi
19、onal windows — for example, a pop-up dialog that calls for a user response in the midst of the activity, or a window that presents users with vital information when they select a particular item on-screen.</p><
20、;p> The visual content of the window is provided by a hierarchy of views — objects derived from the base View class. Each view controls a particular rectangular space within the window. Parent views contain and organ
21、ize the layout of their children. Leaf views (those at the bottom of the hierarchy) draw in the rectangles they control and respond to user actions directed at that space. Thus, views are where the activity's interac
22、tion with the user takes place.</p><p> For example, a view might display a small image and initiate an action when the user taps that image. Android has a number of ready-made views that you can use — incl
23、uding buttons, text fields, scroll bars, menu items, check boxes, and more.</p><p> A view hierarchy is placed within an activity's window by the Activity.setContentView() method. The content view is th
24、e View object at the root of the hierarchy. (See the separate User Interface document for more information on views and the hierarchy.)</p><p><b> Services</b></p><p> A service do
25、esn'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
26、over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class.</p><p> A prime example is a media player playing songs from a play li
27、st. 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
28、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 &l
29、t;/p><p> 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 t
30、he service exposes. For the music service, this interface might allow users to pause, rewind, stop, and restart the playback.</p><p> Like activities and the other components, services run in the main threa
31、d 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.</p&g
32、t;<p> Broadcast receivers</p><p> A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many broadcasts originate in system code — for example, ann
33、ouncements 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 appl
34、ications know that some data has been downloaded to the device and is available for them to use.</p><p> An application can have any number of broadcast receivers to respond to any announcements it consider
35、s important. All receivers extend the BroadcastReceiver base class.</p><p> Broadcast receivers do not display a user interface. However, they may start an activity in response to the information they recei
36、ve, 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 pla
37、ce a persistent icon in the status bar, which users can open to get the message.</p><p> Content providers</p><p> A content provider makes a specific set of the application's data availab
38、le 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
39、 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 </p>&
40、lt;p> See the separate Content Providers document for more information on using content providers.</p><p> Whenever there's a request that should be handled by a particular component, Android makes
41、sure that the application process of the component is running, starting it if necessary, and that an appropriate instance of the component is available, creating the instance if necessary.</p><p> Activatin
42、g components: intents</p><p> Content providers are activated when they're targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are
43、activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data t
44、o act on, among other things. For example, it might convey a request for an activity to present an image to </p><p> Intent object names the action being announced. For example, it might announce to interes
45、ted parties that the camera button has been pressed.</p><p> There are separate methods for activating each type of component:</p><p> 1. An activity is launched (or given something new to do)
46、 by passing an Intent object to</p><p> Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getI
47、ntent() method. Android calls the activity'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 it's starting, it calls
48、 startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a pho</p><p> 2. A service is started (or new instructions are given to an ongoing servic
49、e) by passing an Intent object to Context.startService(). Android calls the service's onStart() method and passes it the Intent object. Similarly, an intent can be passed to Context.bindService() to establish an ongo
50、ing connection between the calling component 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.) Fo</p>
51、<p> A later section, Remote procedure calls, has more details about binding to a service.</p><p> 3. An application can initiate a broadcast by passing an Intent object to methods like Context.sendB
52、roadcast(), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations.</p><p> Android delivers the intent to all interested broadcast receivers by calling their onReceive
53、() methods. For more on intent messages, see the separate article, Intents and Intent Filters.</p><p> Shutting down components</p><p> A content provider is active only while it's respond
54、ing to a request from a ContentResolver. And a broadcast receiver is active only while it's responding to a broadcast message. So there's no need to explicitly shut down these components.</p><p> Ac
55、tivities, on the other hand, provide the user interface. They're in a long-running conversation with the user and may remain active, even when idle, as long as the conversation continues. Similarly, services may also
56、 remain running for a long time. So Android has methods to shut down activities and services in an orderly way:</p><p> 1. An activity can be shut down by calling its finish() method. One activity can shut
57、down another activity (one it started with startActivityForResult()) by calling finishActivity().</p><p> 2. A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().&l
58、t;/p><p> 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 components. A later section, Component Lifecycles, discusses th
59、is possibility and its ramifications in more detail.</p><p> The manifest file</p><p> Before Android can start an application component, it must learn that the component exists. Therefore, ap
60、plications declare their components in a manifest file that's bundled into the Android package, the .apk file that also holds the application's code, files, and resources.</p><p> The manifest is a
61、structured XML file and is always named AndroidManifest.xml for all applications. It does a number of things in addition to declaring the application's components, such as naming any libraries the application needs t
62、o be linked against (besides the default Android library) and identifying any permissions the application expects to be granted.</p><p> But the principal task of the manifest is to inform Android about the
63、 application's components. For example, an activity might be declared as follows:</p><p> The name attribute of the <activity> element names the Activity subclass that implements the activity. The
64、 icon and label attributes point to resource files containing an icon and label that can be displayed to users to represent the activity.</p><p> The other components are declared in a similar way — <ser
65、vice> elements for services, <receiver> elements for broadcast receivers, and <provider> elements for content providers. Activities, services, and content providers that are not declared in the manifest ar
66、e 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 wi
67、th the system by c</p><p> For more on how to structure a manifest file for your application, see The Android Manifest.xml File.</p><p> Intent filters</p><p> An Intent object c
68、an explicitly name a target component. If it does, Android finds that component (based on the declarations in the manifest file) and activates it. But if a target is not explicitly named, Android must locate the best com
69、ponent to respond to the intent. It does so by comparing the Intent object to the intent filters of potential targets. A component's intent filters inform Android of the kinds of intents the component is able to hand
70、le. Like other essential information about the</p><p> The first filter in the example — the combination of the action "android.intent.action.MAIN" and the category</p><p> "and
71、roid.intent.category.LAUNCHER" — is a common one. It marks the activity as one that should be represented in the application launcher, the screen listing applications users can launch on the device. In other words,
72、the activity is the entry point for the application, the initial one users would see when they choose the application in the launcher.</p><p> The second filter declares an action that the activity can perf
73、orm on a particular type of data.</p><p> A component can have any number of intent filters, each one declaring a different set of capabilities. If it doesn't have any filters, it can be activated only
74、by intents that explicitly name the component as the target.</p><p> For a broadcast receiver that's created and registered in code, the intent filter is instantiated directly as an IntentFilter object.
75、 All other filters are set up in the manifest.</p><p> For more on intent filters, see a separate document, Intents and Intent Filters.</p><p> 應用程序基礎Android Developers</p><p> A
76、ndroid應用程序使用Java編程語言開發(fā)。aapt工具把編譯后的Java代碼連同應用程序所需的其他數(shù)據(jù)和資源文件一起打包到一個Android包文件中,這個文件使用.apk作為擴展名。此文件是分發(fā)并安裝應用程序到移動設備的載體;是用戶下載到他們的設備的文件。單一.apk文件中的所有代碼被認為是一個應用程序。</p><p> 從多個角度來看,每個Android應用程序都存在于它自己的世界之中:</p&g
77、t;<p> 1 默認情況下,每個應用程序均運行于它自己的Linux進程中。當應用程序中的任何代碼需要被執(zhí)行時,Android啟動此進程,而當不再需要此進程并且其它應用程序又請求系統(tǒng)資源時,則關閉這個進程。 </p><p> ? 2 每個進程都有其獨有的虛擬機(VM),所以應用程序代碼與所有其它應用程序代碼是隔離運行的。 </p><p> 3 默認情況下,每個應用程
78、序均被賦予一個唯一的Linux用戶ID,并加以權限設置,使得應用程序的文件僅對此用戶及此應用程序可見——盡管也有其它的方法使得這些文件同樣能為其他應用程序所訪問。 </p><p><b> 1 應用程序組件</b></p><p> Android的一個核心特性就是一個應用程序可以使用其它應用程序的元素(如果那個應用程序允許的話)。例如,如果你的應用程序需要顯示
79、一個圖片卷動列表,而另一個應用程序已經開發(fā)了一個合用的而又允許別的應用程序使用的話,你可以直接調用那個卷動列表來完成工作,而不用自己再開發(fā)一個。你的應用程序并沒有吸納或鏈接其它應用程序的代碼。它只是在有需求的時候啟動了其它應用程序的那個功能部分。 </p><p> 為達到這個目的,系統(tǒng)必須能夠在一個應用程序的任何一部分被需要時啟動一個此應用程序的進程,并將那個部分的Java對象實例化。因此,不像其它大多數(shù)系統(tǒng)
80、上的應用程序,Android應用程序并沒有為應用程序提供一個單獨的入口點(比如說,沒有main()函數(shù)),而是為系統(tǒng)提供了可以實例化和運行所需的必備組件。一共有四種組件類型: </p><p> 1 Activity </p><p> activity是為用戶操作而展示的可視化用戶界面。例如,一個activity可以展示一個菜單項列表供用戶選擇,戒者顯示一些包含說明文字的照片。一個短
81、消息應用程序可以包括一個用于顯示要發(fā)送消息到的聯(lián)系人列表的activity,一個給選定的聯(lián)系人寫短信的activity以及翻閱以前的短信或改變設置的其他activity。盡管它們一起組成了一個內聚的用戶界面,但其中每個activity都不其它的保持獨立。每一個都實現(xiàn)為以Activity類為基類的子類。 </p><p> 一個應用程序可以只有一個activity,戒者,如剛才提到的短信應用程序那樣,包含很多個。
82、每個activity的作用,以及有多少個activity,當然是取決于應用程序及其設計的。一般情況下,總有一個應用程序被標記為用戶在應用程序啟動的時候第一個看到的。從一個activity轉向另一個靠的是用當前的activity啟動下一個。 </p><p> 每個activity都被給予一個默認的窗口以進行繪制。一般情況下,這個窗口是滿屏的,但它也可以是一個小的位于其它窗口之上的浮動窗口。一個activity也
83、可以使用附加窗口——例如,一個在activity運行過程中彈出的供用戶響應的對話框,戒是一個當用戶選擇了屏幕上特定項目后顯示的必要信息的窗口。 </p><p> 窗口顯示的可視內容是由一系列層次化view構成的,這些view均繼承自 View 基類。每個view均控制著窗口中一塊特定的矩形區(qū)域。父級view包含并組織其子view的布局。葉節(jié)點view(位于層次結構最底端)在它們控制的矩形區(qū)域中進行繪制,并對用
84、戶直達其區(qū)域的操作做出響應。因此,view是activity與用戶進行交互的界面。例如,view可以顯示一個小圖片,并在用戶指點它的時候產生動作。Android有一些預置的view供開發(fā)者使用——包括按鈕、文本域、滾動條、菜單項、復選框等等。 </p><p> view層次結構是由Activity.setContentView() 方法放入activity的窗口之中的。content view是位于層次結構根
85、位置的View對象。(參見獨立的用戶界面文檔以獲取關于view及層次結構的更多信息。) </p><p> 2 Service </p><p> service沒有可視化的用戶界面,而是在一段時間內在后臺運行。例如,一個service可以在用戶做其它事情的時候在后臺播放背景音樂、從網(wǎng)絡上獲取數(shù)據(jù)或者計算一些東西并提供給需要這個運算結果的activity使用。每個service都繼承自
86、Service基類。 </p><p> 一個媒體播放器播放播放列表中的曲目是一個不錯的例子。播放器應用程序可能有一個或多個activity來給用戶選擇歌曲并進行播放。然而,音樂播放這個任務本身丌應該由任何activity來處理,因為用戶期望即使在他們離開播放器應用程序而開始做別的事情時,音樂仍在繼續(xù)播放。為達到這個目的,媒體播放器activity可以啟動一個運行于后臺的service。系統(tǒng)將在這個activi
87、ty不再顯示于屏幕乀后,仍維持音樂播放service的運行。 </p><p> 連接至(綁定到)一個正在運行的service(如果service沒有運行,則啟動之)是可能的。連接之后,你可以通過那個service暴露出來的接口不service進行通訊。對于音樂service來說,這個接口可以允許用戶暫停、回退、停止以及重新開始播放。 </p><p> 如同activity和其它組件
88、一樣,service運行于應用程序進程的主線程內。所以它不會對其它組件或用戶界面有任何妨礙,它們一般會派生一個新線程來執(zhí)行一些時間消耗型任務(比如音樂回放)。參見稍后的進程和線程。 </p><p> 3 Broadcast receiver </p><p> broadcast receiver是一個與注于接收廣播通知信息,并做出相應處理的組件。許多廣播是由系統(tǒng)代碼產生的——例如,
89、通知時區(qū)改變、電池電量低、拍攝了一張照片或者用戶改變了語言選項。應用程序也可以發(fā)起廣播——例如,通知其它應用程序一些數(shù)據(jù)已經下載到設備上并處于可用狀態(tài)。 </p><p> 一個應用程序可以擁有任意數(shù)量的broadcast receiver,以對所有它認為重要的通知信息予以響應。所有的receiver均繼承自BroadcastReceiver基類。 </p><p> broadcas
90、t receiver沒有用戶界面。然而,它們可以啟動一個activity來響應它們收到的信息,或者也可以使用NotificationManager來通知用戶。通知可以用多種方式來吸引用戶的注意力──閃動背光燈、震動設備、播放聲音等等。通知一般是在狀態(tài)欄上放一個持麗的圖標,用戶可以打開它并獲取消息。 </p><p> 4 Content provider </p><p> conte
91、nt provider將一些特定的應用程序數(shù)據(jù)供給其它應用程序使用。數(shù)據(jù)可以存儲于文件系統(tǒng)、SQLite數(shù)據(jù)庫或其它有意丿的方式。content provider繼承于ContentProvider 基類,實現(xiàn)了一套使得其他應用程序能夠檢索和存儲它所管理的類型數(shù)據(jù)的標準方法。然而,應用程序并不直接調用返些方法,而是使用一個 ContentResolver 對象,調用它的方法作為替代。ContentResolver可以與任何content
92、 provider進行會話;與其合作對任何相關的進程間通訊進行管理。 </p><p> 參閱獨立的Content Providers文檔以獲得更多關于使用content provider的信息。 </p><p> 每當出現(xiàn)一個需要被特定組件處理的請求時,Android會確保那個組件的應用程序進程處于運行狀態(tài),必要時會啟動它,并確保那個組件的一個合適的實例可用,必要時會創(chuàng)建那個實例。
93、 </p><p> 1.1激活組件:intent</p><p> 當接收到ContentResolver發(fā)出的請求后,content provider被激活。而其它三種組件——activity、service和broadcast receiver,被一種叫做intent的異步消息所激活。intent是一個保存著消息內容的Intent對象。對于activity和service來說,它
94、指明了所請求的操作名稱,并指定了用來操作的數(shù)據(jù)的URI和其它一些信息。例如,它可以承載一個對一個activity的請求,讓它為用戶顯示一張圖片,或者讓用戶編輯一些文本。而對于broadcast receiver來說,Intent對象指明了所通報的操作。例如,它可以對所有感興趣的對象通報照相按鈕被按下。 </p><p> 對于每種組件來說,激活的方法是不同的: </p><p> ?
95、 1 通過傳遞一IntentContext.startActivity()Activity.startActivityForResult(以啟動(或指定新工作給)一個activity。相應的activity可以通過調用自身的 getIntent() 方法來查看最刜激活它的intent。Android通過調用activity的onNewIntent()方法來傳遞給它隨后的任何intent。</p><p>
96、一個activity經常啟動另一個activity。如果它期望它所啟動的那個activity迒回一個結果,它會調用startActivityForResult()而不是startActivity()。例如,如果它啟動了另外一個activity以使用戶挑選一張照片,它也許想知道哪張照片被選中了。其結果將會被封裝在一個Intent對象中,并傳遞給發(fā)出調用的activity的onActivityResult() 方法。 </p>
97、<p> 2 通過傳遞一個Intent對象至Context.startService()以啟動一個service(或向正在運行的service給出一個新的指令)。Android調用此service的 onStart()方法并將Intent對象傳遞給它。 </p><p> 與此類似,一個intent可以被傳遞給 Context.bindService()以建立一個處于調用組件和目標service乀
98、間的活動連接。此service會通過onBind() 方法的調用來獲取此Intent對象(如果此service尚未運行,bindService()會先啟動它)。例如,一個activity可以建立一個不前述的音樂回放service的連接,這樣它就可以提供給用戶一些途徑(用戶界面)來控制回放。這個activity可以調用 bindService()來建立此連接,然后調用service中定之的方法來控制回放。 </p><
99、p> 稍后的遠程方法調用一節(jié)有關于如何綁定至一個service的更多細節(jié)。 </p><p> ? 3 應用程序可以通過傳遞一個Intent對象至 Context.sendBroadcast() ,Context. sendOrderedBroadcast(), 以及Context.sendStickyBroadcast()和其它類似方法來發(fā)起一個廣播。Android會調用所有對此廣播有興趣的br
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- android手機外文翻譯---應用程序基礎android developers
- android應用程序基礎
- [雙語翻譯]安卓外文翻譯--android應用程序的安全性
- [雙語翻譯]安卓外文翻譯--android應用程序的安全性(英文)
- 2016年安卓外文翻譯--android應用程序的安全性
- [雙語翻譯]安卓外文翻譯--android應用程序的安全性中英全
- 2016年安卓外文翻譯--Android應用程序的安全性.DOCX
- 應用程序基礎——論文
- 2016年安卓外文翻譯--Android應用程序的安全性(英文).PDF
- 外文翻譯--mysql和jsp的web應用程序
- 開發(fā)xml應用程序-畢業(yè)論文外文翻譯
- 基于asp的web應用程序開發(fā)外文翻譯
- 外文翻譯---web 應用程序的基本安全做法
- Android系統(tǒng)移植和應用程序開發(fā).pdf
- Android應用程序權限檢測機制的研究.pdf
- Android應用程序組件漏洞測試方法研究.pdf
- Android應用程序安全分析技術研究.pdf
- 基于Android系統(tǒng)的示波器應用程序開發(fā).pdf
- Android應用程序安全的分析方法研究.pdf
- 基于靜態(tài)程序分析方法的ANDROID應用程序行為審計.pdf
評論
0/150
提交評論