畢業(yè)論文外文翻譯-Android應(yīng)用程序_第1頁(yè)
畢業(yè)論文外文翻譯-Android應(yīng)用程序_第2頁(yè)
畢業(yè)論文外文翻譯-Android應(yīng)用程序_第3頁(yè)
畢業(yè)論文外文翻譯-Android應(yīng)用程序_第4頁(yè)
畢業(yè)論文外文翻譯-Android應(yīng)用程序_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、本科畢業(yè)設(shè)計(jì)(論文)外文參考文獻(xiàn)譯文及原文測(cè)控技術(shù)與儀器(光機(jī)電一體化方向)年級(jí)班別 學(xué) 號(hào) 學(xué)生姓名 指導(dǎo)教師2015年6月1應(yīng)用程序基礎(chǔ)11.1應(yīng)用程序組件11.2 激活組件:intent31.3關(guān)閉組件41.4 manifest 文件51.5 intent 過(guò)濾器61.6 基于xml的布局71 application fundamenta is 81.1 application components81.2 activating components:intent111.3 shutting down components121.4 the manifest file131.5 inte

2、nt filters141.6 xml-based layout151應(yīng)用程序基礎(chǔ)android應(yīng)用程序使用java編程語(yǔ)言開(kāi)發(fā)。aapt工具把編譯后的java代碼連同 應(yīng)用程序所需的其他數(shù)據(jù)和資源文件一起打包到一個(gè)android包文件屮,這個(gè)文件 使用.apk作為擴(kuò)展名。此文件是分發(fā)并安裝應(yīng)用程序到移動(dòng)設(shè)備的載體;是用戶下 載到他們的設(shè)備的文件。單一.apk文件中的所有代碼被認(rèn)為是一個(gè)應(yīng)用程序。從多個(gè)角度來(lái)看,每個(gè)android應(yīng)用程序都存在于它自己的世界z屮:1默認(rèn)情況下,每個(gè)應(yīng)用程序均運(yùn)行于它白己的linux進(jìn)程中。當(dāng)應(yīng)用程序屮 的任何代碼需要被執(zhí)行時(shí),android啟動(dòng)此進(jìn)程,而當(dāng)不再

3、需要此進(jìn)程并且其它應(yīng) 用程序又請(qǐng)求系統(tǒng)資源時(shí),則就關(guān)閉了這個(gè)進(jìn)程。2每個(gè)進(jìn)程都有其獨(dú)有的虛擬機(jī)(vm),所以應(yīng)用程序代碼與所有其它應(yīng)用程 序代碼是隔離運(yùn)行的。3默認(rèn)情況下,每個(gè)應(yīng)用程序均被賦予一個(gè)唯一的linux用戶id,并加以權(quán) 限設(shè)置,使得應(yīng)用程序的文件僅對(duì)此用戶及此應(yīng)用程序可見(jiàn)一一盡管也有其它的方 法使得這些文件同樣能為其他應(yīng)用程序所訪問(wèn)。1.1應(yīng)用程序組件android的一個(gè)核心的特性就是一個(gè)應(yīng)用程序可以使用其它應(yīng)用程序的元素(如果那個(gè)應(yīng)用程序允許的話)。例如,如果你的應(yīng)用程序需要顯示一個(gè)圖片卷動(dòng) 列表,而另一個(gè)應(yīng)用程序已經(jīng)開(kāi)發(fā)了一個(gè)合用的而又允許別的應(yīng)用程序使用的話, 你可以直接調(diào)用

4、那個(gè)卷動(dòng)列表來(lái)完成工作,而不用自己再開(kāi)發(fā)一個(gè)。你的應(yīng)用程序 并沒(méi)有吸納或鏈接其它應(yīng)用程序的代碼。它只是在有需求的時(shí)候啟動(dòng)了其它應(yīng)用程 序的那個(gè)功能部分。為達(dá)到這個(gè)目的,系統(tǒng)必須能夠在一個(gè)應(yīng)用程序的任何一部分被需要時(shí)啟動(dòng)一 個(gè)此應(yīng)用程序的進(jìn)程,并將那個(gè)部分的java對(duì)象實(shí)例化。因此,不像其它大多數(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)行所需的必備組件。 一共有四種組件類(lèi)型:(l) activityactivity是為用戶操作而展示的可視化用戶界面。例如,一個(gè)activity可以展示 一個(gè)菜單

5、項(xiàng)列表供用戶選擇,戒者顯示一些包含說(shuō)明文字的照片。一個(gè)短消息應(yīng)用 程序可以包括一個(gè)用于顯示要發(fā)送消息到的聯(lián)系人列表的activity, 一個(gè)給選定的 聯(lián)系人寫(xiě)短信的activity以及翻閱以前的短信或改變?cè)O(shè)置的其他activityo盡管它們 一起組成了一個(gè)內(nèi)聚的用戶界面,但其屮每個(gè)activity都不其它的保持獨(dú)立。每一 個(gè)都實(shí)現(xiàn)為以activity類(lèi)為基類(lèi)的子類(lèi)。一個(gè)應(yīng)用程序可以只有一個(gè)activity,戒者,如剛才提到的短信應(yīng)用程序那樣, 包含很多個(gè)。每個(gè)activity的作用,以及有多少個(gè)activity,當(dāng)然是取決于應(yīng)用程序 及其設(shè)計(jì)的。一般情況下,總有一個(gè)應(yīng)用程序被標(biāo)記為用戶在應(yīng)用程

6、序啟動(dòng)的時(shí)候 第一個(gè)看到的。從一個(gè)activity轉(zhuǎn)向另一個(gè)靠的是用當(dāng)前的activity啟動(dòng)下一個(gè)。每個(gè)activity都被給予一個(gè)默認(rèn)的窗口以進(jìn)行繪制。一般情況下,這個(gè)窗口是 滿屏的,但它也可以是一個(gè)小的位于其它窗口之上的浮動(dòng)的窗口。一個(gè)activity也 可以使用附加的窗口一一例如,一個(gè)在activity運(yùn)行過(guò)程中彈岀的供用戶響應(yīng)的對(duì) 話框,這是一個(gè)當(dāng)用戶選擇了屏幕上特定項(xiàng)目后顯示的必要信息的窗口。窗口顯示的可視內(nèi)容是由一系列層次化view構(gòu)成的,這些view均繼承自view 基類(lèi)。每個(gè)view均控制著窗口中一塊特定的矩形區(qū)域。父級(jí)view包含并組織其子 view的布局。葉節(jié)點(diǎn)view

7、(位于層次結(jié)構(gòu)最底端)在它們控制的矩形區(qū)域中進(jìn)行 繪制,并對(duì)用戶直達(dá)其區(qū)域的操作做出響應(yīng)。因此,view是activity與用戶進(jìn)行交 互的界而。例如,view可以顯示一個(gè)小圖片,并在用戶指點(diǎn)它的時(shí)候產(chǎn)生動(dòng)作。 android有一些預(yù)置的view供開(kāi)發(fā)者使用一一包括按鈕、文本域、滾動(dòng)條、菜單項(xiàng)、 復(fù)選框等等。view的層次結(jié)構(gòu)是由activity.setcontentview()方法放入activity的窗口之中 的。content view是位于層次結(jié)構(gòu)根位置的view對(duì)象。(參見(jiàn)獨(dú)立的用戶界而文檔 以獲取關(guān)于view及層次結(jié)構(gòu)的更多信息。)(2)serviceservice沒(méi)有可視化的用戶

8、界而,而是在一段時(shí)間內(nèi)在后臺(tái)運(yùn)行。例如,一個(gè) service可以在用戶做其它事情的時(shí)候在后臺(tái)播放背景音樂(lè)、從網(wǎng)絡(luò)上獲取數(shù)據(jù)或者 計(jì)算一些東西并提供給需要這個(gè)運(yùn)算結(jié)果的activity使用。每個(gè)service都繼承自 service 基類(lèi)。一個(gè)媒體播放器播放播放列表中的曲目是一個(gè)不錯(cuò)的例子。播放器應(yīng)用程序可 能有一個(gè)或多個(gè)activity來(lái)給用戶選擇歌曲并進(jìn)行播放。然而,音樂(lè)播放這個(gè)任務(wù) 本身開(kāi)應(yīng)該由任何activity來(lái)處理,因?yàn)橛脩舻钠谕词乖谒麄冸x開(kāi)播放器的應(yīng)用 程序而已經(jīng)在開(kāi)始做別的事情時(shí),音樂(lè)仍然在繼續(xù)播放。為達(dá)到這個(gè)冃的,媒體播 放器activity可以啟動(dòng)一個(gè)運(yùn)行于后臺(tái)的servi

9、ce服務(wù)。系統(tǒng)將在這個(gè)activity不再 顯示于屏幕后,仍維持音樂(lè)播放的service的運(yùn)行。連接至(綁定到)一個(gè)正在運(yùn)行的service (如果service沒(méi)有運(yùn)行,則啟動(dòng)之) 是可能的。連接之后,你可以通過(guò)那個(gè)service暴露出來(lái)的接口不service進(jìn)行通訊。 對(duì)于音樂(lè)service來(lái)說(shuō),這個(gè)接口可以允許用戶暫停、回退、停止以及重新開(kāi)始播 放。如同activity和其它的組件一樣,service服務(wù)運(yùn)行于應(yīng)用程序進(jìn)程的主線程內(nèi)。 所以它不會(huì)對(duì)其它組件或者用戶界面有任何的妨礙作用,它們一般會(huì)派生一個(gè)新線 程來(lái)執(zhí)行一些時(shí)間消耗型任務(wù)(比如音樂(lè)回放和網(wǎng)絡(luò)下載)。參見(jiàn)稍后的進(jìn)程和線 程介紹

10、。(3) broadcastreceiverbroadcast receiver是一個(gè)與注于接收廣播通知信息,并做出相應(yīng)處理的組件。 許多廣播是由系統(tǒng)代碼產(chǎn)生的一一例如,通知時(shí)區(qū)改變、電池電量低、拍攝了一張 照片或者用戶改變了語(yǔ)言選項(xiàng)。應(yīng)用程序也可以發(fā)起廣播一一例如,通知其它應(yīng)用 程序一些數(shù)據(jù)已經(jīng)下載到設(shè)備上并處于可用狀態(tài)。一個(gè)應(yīng)用程序可以擁有任意數(shù)量的broadcast receiver,以對(duì)所有它認(rèn)為重要的 通知信息予以各種響應(yīng)。所有的receiver均繼承自broadcastreceiver基類(lèi)。broadcast receiver沒(méi)有用戶界而。然而,它們可以啟動(dòng)一個(gè)activity或者

11、service 來(lái)響應(yīng)它們收到的信息,當(dāng)然也可以使用notificationmanager來(lái)通知用戶。通知可 以用多種方式來(lái)吸引用戶的注意力閃動(dòng)背光燈、震動(dòng)設(shè)備、播放聲音等等。通 知一般是在狀態(tài)欄上放一個(gè)持麗的圖標(biāo),用戶可以點(diǎn)擊打開(kāi)它并獲取所要消息。(4) contentprovidercontent provider將一些特定的應(yīng)用程序數(shù)據(jù)供給其它應(yīng)用程序使用處理。數(shù)據(jù) 可以存儲(chǔ)于文件系統(tǒng)、sqlite數(shù)據(jù)庫(kù)或其它有意丿的方式。content provider繼承于 contentprovider基類(lèi),實(shí)現(xiàn)了一套使得其他應(yīng)用程序能夠檢索和存儲(chǔ)它所管理的類(lèi) 型數(shù)據(jù)的標(biāo)準(zhǔn)方法。然而,應(yīng)用程序并不

12、直接調(diào)用返些方法,而是使用一個(gè) contentresolver對(duì)彖,調(diào)用它的方法作為替代o contentresolver可以與任何content provider進(jìn)行會(huì)話;與其合作對(duì)任何相關(guān)的進(jìn)程間通訊進(jìn)行管理。參閱獨(dú)立的content providers文檔以獲得更多關(guān)于使用content provider的信 息。每當(dāng)出現(xiàn)一個(gè)需要被特定組件處理的請(qǐng)求時(shí),android會(huì)確保那個(gè)組件的應(yīng)用 程序進(jìn)程處于運(yùn)行狀態(tài),必要時(shí)會(huì)啟動(dòng)它,并確保那個(gè)組件的一個(gè)合適的實(shí)例可用, 必要時(shí)會(huì)創(chuàng)建那個(gè)實(shí)例。12激活組件:intent當(dāng)接收到contentresolver發(fā)出的請(qǐng)求后,content provi

13、der被激活。而其它三種 組件 activity> service和broadcast receiver,被一種叫做intent的異步消息所激 活。intent是一個(gè)保存著消息內(nèi)容的intent對(duì)象。對(duì)于activity和service來(lái)說(shuō),它 指明了所請(qǐng)求的操作名稱,并指定了用來(lái)操作的數(shù)據(jù)的uri和其它一些信息。例 如,它可以承載一個(gè)對(duì)一個(gè)activity的請(qǐng)求,讓它為用戶顯示一張圖片,或者讓用 戶編輯一些文本。而對(duì)于broadcast receiver來(lái)說(shuō),intent對(duì)彖指明了所通報(bào)的操作。 例如,它可以對(duì)所有感興趣的對(duì)象通報(bào)照相按鈕被按下。對(duì)于每種組件來(lái)說(shuō),激活的方法也是不同的:

14、1 通過(guò)傳遞一 intentcontext.startactivity()activity.startactivityforresult(以啟動(dòng) (或指定新工作給)另外一個(gè)activityo相應(yīng)的activity可以通過(guò)調(diào)用自身的 getlntent()方法來(lái)查看并且激活它的intento android通過(guò)調(diào)用activity的 onnewintent()方法來(lái)傳遞給它隨后的任何intento一個(gè)activity經(jīng)常啟動(dòng)另一個(gè)activityo如果它期望它所啟動(dòng)的那個(gè)activity 返回一個(gè)結(jié)果,它會(huì)調(diào)用startactivityforresult()而不是start activity (

15、)o例如,如果 它啟動(dòng)了另外一個(gè)activity以使用戶挑選一張自己的照片,它也許想知道哪張照片 被選中了。其結(jié)果將會(huì)被封裝在一個(gè)intent對(duì)象中,并傳遞給發(fā)岀調(diào)用的activity 的 onactivityresult()方法。2通過(guò)傳遞一個(gè)intent對(duì)象至context.startservice()以啟動(dòng)一個(gè)service (或向正 在運(yùn)行的service給出一個(gè)新的指令)。android調(diào)用此service的onstart()方法并將 intent對(duì)彖傳遞給它。與此類(lèi)似,一個(gè)intent可以被傳遞給context.bindservice()以建立一個(gè)處于調(diào)用 組件和冃標(biāo)service

16、 j間的活動(dòng)連接。此service會(huì)通過(guò)onbind()方法的調(diào)用來(lái)獲取 此intent對(duì)象(如果此service尚未運(yùn)行,bindservice()會(huì)先啟動(dòng)它)。例如,一個(gè) activity可以建立一個(gè)不前述的音樂(lè)回放service的連接,這樣它就可以提供給用戶 一些途徑(用戶界面)來(lái)控制回放。這個(gè)activity可以調(diào)用bindservice()來(lái)建立此 連接,然后調(diào)用service中定之的方法來(lái)控制回放。稍后的遠(yuǎn)程方法調(diào)用一節(jié)有關(guān)于如何綁定至一個(gè)service的更多細(xì)節(jié)。3應(yīng)用程序可以通過(guò)傳遞一個(gè)intent對(duì)彖至context.sendbroadcast() , context. se

17、ndorderedbroadcast(),以及 context.sendstickybroadcast()和其它類(lèi)似方法來(lái)發(fā)起 一個(gè)廣播。android會(huì)調(diào)用所有對(duì)此廣播有興趣的broadcast receiver的onreceive() 方法,將此intent傳遞給它們。1.3關(guān)閉組件content provider僅在響應(yīng)來(lái)自contentresolver的請(qǐng)求時(shí)處于不同活動(dòng)狀態(tài)。而 broadcast receiver僅在響應(yīng)一條廣播信息的時(shí)候處于各種活動(dòng)狀態(tài)。所以沒(méi)有必要 去顯式地關(guān)閉這組件。而activity則不同,它提供了用戶界而。只要會(huì)話依然持續(xù),無(wú)論會(huì)話過(guò)程有 無(wú)空閑,acti

18、vity同用戶進(jìn)行長(zhǎng)時(shí)間會(huì)話且可能一直處于活動(dòng)狀態(tài)。與此相似,service 也會(huì)在很長(zhǎng)一段時(shí)間內(nèi)在后臺(tái)保持運(yùn)行。所以android為關(guān)閉activity和service提 供了一系列有序的方法。activity可以通過(guò)調(diào)用自身的finish。方法來(lái)關(guān)閉。一個(gè)activity可以通過(guò)調(diào)用 finishactivity()方法來(lái)關(guān)閉另夕卜一個(gè) activity (它用 startactivityforresult()啟動(dòng)的)。service可以通過(guò)調(diào)用自身的stopself()方法,或調(diào)用context.stopservice() 來(lái)停止。系統(tǒng)也會(huì)在組件不再被使用的時(shí)候戒者當(dāng)android必須為

19、更多的活動(dòng)組件回收 內(nèi)存時(shí)關(guān)閉它。稍后的組件的生命的周期一節(jié),將對(duì)這種可能性及結(jié)果進(jìn)行更詳細(xì) 的介紹討論。1.4 manifest 文件當(dāng)android啟動(dòng)一個(gè)應(yīng)用程序組件之前,它必須知道那個(gè)組件是存在的。因此, 應(yīng)用程序會(huì)在一個(gè)被打包到android包中的manifest文件中聲明它的組件,.apk文 件還將涵括應(yīng)用程序的代碼、文件以及其它資源。manifest文件是一個(gè)結(jié)構(gòu)化的xml文件,而且對(duì)于所有應(yīng)用程序,文件名總 是androidmanifest.xml除了聲明此應(yīng)用程序各個(gè)組件,它會(huì)做很多其他工作,比 如指明應(yīng)用程序所需鏈接到的庫(kù)的名稱(除了默認(rèn)的android庫(kù)外)以及標(biāo)出應(yīng)用

20、程序期望獲得的各種權(quán)限。但manifest文件最重要的任務(wù)是向android報(bào)告此應(yīng)用程序的各個(gè)組件。丼例 說(shuō)明,一個(gè)activity可能聲明如下:<?xml version=n1.0" encoding='utf-8"?>manifest. ><application .><activityandroid:name=,ject.freneticactmtynandroid:icon=ndrawable/5mall_pic.png" androidjabe'string/frenet

21、iclaber .></activity> </application></manifest><activity>元素的name屬性指定了實(shí)現(xiàn)此activity的activity子類(lèi)。icon和label 屈性指向包含展示給用戶的此activity的圖標(biāo)和標(biāo)簽的資源文件。其它組件也以類(lèi)似的方法聲明<service>元素用于聲明service,<receiver> 元素用于聲明 broadcast receiver, 而 <provider> 元素用于聲明 content providero 未在 mani

22、fest 文件中進(jìn)行聲明的 activity> service 以及 content provider 將 不為系統(tǒng)所見(jiàn),從而也就永不會(huì)被運(yùn)行。然而,broadcast receiver既可以在manifest 文件中聲明,也可以在代碼中動(dòng)態(tài)創(chuàng)建(為broadcastreceiver對(duì)彖),并以調(diào)用 context.registerreceiver()的方式注冊(cè)至系統(tǒng)。1.5 intent過(guò)濾器一個(gè)intent對(duì)象可以顯式地指定一個(gè)目標(biāo)組件。如果進(jìn)行了返種指定,android 會(huì)找到這個(gè)組件(基于manifest文件中的聲明)并激活它。但如果intent沒(méi)有顯式 地指定一個(gè)冃標(biāo),andr

23、oid就必須找到最合適的組件來(lái)響應(yīng)此intento這個(gè)過(guò)程是通 過(guò)比較intent對(duì)象和所有潛在冃標(biāo)的intent過(guò)濾器完成的。組件的intent過(guò)濾器會(huì) 通知android它所能處理的intent類(lèi)型。如同組件的其它必要信息一樣,這些intent 過(guò)濾器是在manifest文件中進(jìn)行聲明的。返里有一個(gè)對(duì)先前例子的擴(kuò)展,其中加入 了針對(duì)activity的兩個(gè)intent過(guò)濾器:<?xml version=,'1.0" encoding=,utf-8"?><manifest. >application .><activitvandr

24、oid:name="ject.freneticactivity" android:icon="drawable/smallj)ic.png" android:label=nstring/freneticlaber.><intent-filter .><action android:name="ent.action.main" />< category android:name=ent.category.launcher&qu

25、ot; /></intent-filter><intent-filter .>action android:name="ject.bounce" /><data android:mimetype="image/jpeg,1 /><category dndroid:name="ent.category.default" /></intent-filter></activity> </application&

26、gt;</manife5t>示例中的第一個(gè)過(guò)濾器action ent.action.main n 和category ent.category.launchern 的組合是常見(jiàn)的一個(gè)。它標(biāo)明了此activity應(yīng)該在應(yīng)用程序啟動(dòng)器中顯示,就是用戶在屏幕上看到的此設(shè)備上可 供啟動(dòng)的應(yīng)用程序的列表。換句話說(shuō),這個(gè)activity是應(yīng)用程序的入口點(diǎn),是用戶 在啟動(dòng)器中選擇運(yùn)行這個(gè)應(yīng)用程序后所見(jiàn)到的第一個(gè)activityo第二個(gè)過(guò)濾器聲明了此activity在一種特定類(lèi)型的數(shù)據(jù)上可以執(zhí)行的操作。一個(gè)組件可以擁有任意數(shù)量的intent過(guò)濾器,每

27、個(gè)都聲明了一套不同的功能。 如果組件沒(méi)有包含任何過(guò)濾器,它只能被顯式地指明作為冃標(biāo)組件的intent激活。對(duì)于在代碼中創(chuàng)建并注冊(cè)的broadcast receiver來(lái)說(shuō),intent過(guò)濾器將被直接實(shí) 例化intentfilter為對(duì)象。其它所有的過(guò)濾器都在manifest文件中設(shè)置。1.6基于xml的布局雖然純粹通過(guò)java代碼在activity ±創(chuàng)建和添加部件,在技術(shù)上是可行的,我 們?cè)诘?章中做的一樣,更常見(jiàn)的方法是使用一種基于xml的布局文件。動(dòng)態(tài)的 小部件實(shí)例保留更多,情況復(fù)雜,小工具在編譯時(shí)不為人所知(例如,在數(shù)據(jù)檢索 了互聯(lián)網(wǎng)基礎(chǔ)上將單選按鈕填充柱??紤]到這一點(diǎn),現(xiàn)在

28、是時(shí)候打破xml來(lái)學(xué)習(xí)如何用此種方式來(lái)布置android activities o正如其名稱所示,一個(gè)基于xml的布局是一個(gè)關(guān)系到每個(gè)規(guī)格的小部件,和 他們的容器(更多關(guān)于此內(nèi)容的在第7章)編碼的xml格式。具體來(lái)說(shuō),android 認(rèn)為基于xml的布局是資源,因此布局的文件存儲(chǔ)在res/在你的android項(xiàng)冃布 局目錄中。每個(gè)xml文件包含一個(gè)指定的部件和容器布局元素樹(shù),一種意見(jiàn)認(rèn)為構(gòu)成層 次。對(duì)xml元素的屬性,描述一個(gè)部件應(yīng)如何看或者一個(gè)容器應(yīng)如何運(yùn)轉(zhuǎn)。例如, 如果一個(gè)按鈕元素。有一個(gè)android的屬性值:文字樣式二“bold”,這意味著該文本出現(xiàn)在按鈕的 表而應(yīng)該是呈現(xiàn)一個(gè)粗體字體

29、樣式.android的sdk中附帶一個(gè)使用的布局的工具(aapt)。這個(gè)工具應(yīng)自動(dòng)調(diào)用 你的android x具鏈(例如,eclipse中,ant' s build.xmdo作為一個(gè)開(kāi)發(fā)人員, 尤其重要的是,在您的項(xiàng)目中aapt生成r.java源文件,讓您能在那些布局中直接 從java代碼中獲取布局和部件。xml作為一個(gè)gui定義格式是越來(lái)越流行普遍。微軟的xaml, adobe的flex, 和mozilla的xul都采取android類(lèi)似的方法:把布局細(xì)節(jié)放在一個(gè)xml文件和 把編程智慧資料放在源文件(例如,xul中的javascript)o許多不太知名的圖形 用戶界面框架,如zk,

30、還使用視圖定義的xmlo而“隨大流”并不一定是最好的 政策,但他們有優(yōu)勢(shì)幫助從任何其他xml為中心的觀點(diǎn)描述語(yǔ)言輕松進(jìn)入 android o1 application fundamentalsandroid applications are written in the java programming language. the compiled java code 一 along with any data and resource files required by the application 一 is bundled by the aapt tool into an android

31、 package, an archive file marked by an .apk suffix. this file is the vehicle for distributing the application and installing it on mobile devices; its 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 applicati

32、on lives in its own world:1. by default, every 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 its no longer needed and system resources are required by other applications.2. each proce

33、ss 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 application's files are visible only to that user and only to the application its

34、elf 一 although there are ways to export them to other applications as well.it's possible to arrange for two applications to share the same user id, in which case they will be able to see each othefs files. to conserve system resources, applications with the same id can also arrange to run in the

35、 same linux process, sharing the same vm1>1 application componentsa central feature of android is that one application can make 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 a

36、pplication has developed a suitable scroller and 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 starts up that piece of the other ap

37、plication 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 don't have a single entry point for e

38、verything in the application (no main() function, for example). rather, they have essential components that the system can instantiate and run as needed. there are fourtypes of components:(1) activitiesan activity presents a visual user interface for one focused endeavor the user can undertake. for

39、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 contac

40、t, 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.an application might consist of just one activity or, like the

41、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 activities is marked as the first one that should be presented to the user when the application is launched.

42、 moving from one activity to another is accomplished by having the current activity start the next one.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

43、of additional 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.the visual content of the window is provided by a hierarchy of views 一 objects derived

44、 from the base view class. each view controls a particular rectangular space within the window. parent views contain and organize 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. th

45、us, views are where the activity'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 ready-made views that you can use 一 including buttons, text fields, scroll bars, menu items, chec

46、k boxes, and more.a view hierarchy is placed within an activity's window by the activity.setcontentview() method. the content view is the view object at the root of the hierarchy. (see the separate user interface document for more information on views and the hierarchy.)(2) servicesa service doe

47、sn'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

48、 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 handl

49、ed 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

50、after the activity that started it leaves the screen.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

51、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 t

52、asks (like music playback). see processes and threads, later.(3) broadcast receiversa 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 ba

53、ttery 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

54、 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 notificationman

55、ager to alert the use匸 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 themessage.(4) content providersa content provider m

56、akes 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

57、 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 i

58、nterprocess communication that's involved.see the separate content providers document for more information on using content providers.whenever therefs a request that should be handled by a particular component, android makes sure that the application process of the component is running, starting

59、 it if necessary, and that an appropriate instance of the component is available, creating the instance if necessary.1.2 activating components: intentscontent providers are activated when they're targeted by a request from a contentresolver. the other three components 一 activities, services, and broadcast receivers 一 are 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 to act on, among other things. for e

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論