畢業論文外文翻譯-Android應用程序_第1頁
畢業論文外文翻譯-Android應用程序_第2頁
畢業論文外文翻譯-Android應用程序_第3頁
畢業論文外文翻譯-Android應用程序_第4頁
畢業論文外文翻譯-Android應用程序_第5頁
已閱讀5頁,還剩15頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、本科畢業設計(論文)外文參考文獻譯文及原文測控技術與儀器(光機電一體化方向)年級班別 學 號 學生姓名 指導教師2015年6月1應用程序基礎11.1應用程序組件11.2 激活組件:intent31.3關閉組件41.4 manifest 文件51.5 intent 過濾器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應用程序基礎android應用程序使用java編程語言開發。aapt工具把編譯后的java代碼連同 應用程序所需的其他數據和資源文件一起打包到一個android包文件屮,這個文件 使用.apk作為擴展名。此文件是分發并安裝應用程序到移動設備的載體;是用戶下 載到他們的設備的文件。單一.apk文件中的所有代碼被認為是一個應用程序。從多個角度來看,每個android應用程序都存在于它自己的世界z屮:1默認情況下,每個應用程序均運行于它白己的linux進程中。當應用程序屮 的任何代碼需要被執行時,android啟動此進程,而當不再

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

4、那個卷動列表來完成工作,而不用自己再開發一個。你的應用程序 并沒有吸納或鏈接其它應用程序的代碼。它只是在有需求的時候啟動了其它應用程 序的那個功能部分。為達到這個目的,系統必須能夠在一個應用程序的任何一部分被需要時啟動一 個此應用程序的進程,并將那個部分的java對象實例化。因此,不像其它大多數 系統上的應用程序,android應用程序并沒有為應用程序提供一個單獨的入口點(比 如說,沒有main()函數),而是為系統提供了可以實例化和運行所需的必備組件。 一共有四種組件類型:(l) activityactivity是為用戶操作而展示的可視化用戶界面。例如,一個activity可以展示 一個菜單

5、項列表供用戶選擇,戒者顯示一些包含說明文字的照片。一個短消息應用 程序可以包括一個用于顯示要發送消息到的聯系人列表的activity, 一個給選定的 聯系人寫短信的activity以及翻閱以前的短信或改變設置的其他activityo盡管它們 一起組成了一個內聚的用戶界面,但其屮每個activity都不其它的保持獨立。每一 個都實現為以activity類為基類的子類。一個應用程序可以只有一個activity,戒者,如剛才提到的短信應用程序那樣, 包含很多個。每個activity的作用,以及有多少個activity,當然是取決于應用程序 及其設計的。一般情況下,總有一個應用程序被標記為用戶在應用程

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

7、(位于層次結構最底端)在它們控制的矩形區域中進行 繪制,并對用戶直達其區域的操作做出響應。因此,view是activity與用戶進行交 互的界而。例如,view可以顯示一個小圖片,并在用戶指點它的時候產生動作。 android有一些預置的view供開發者使用一一包括按鈕、文本域、滾動條、菜單項、 復選框等等。view的層次結構是由activity.setcontentview()方法放入activity的窗口之中 的。content view是位于層次結構根位置的view對象。(參見獨立的用戶界而文檔 以獲取關于view及層次結構的更多信息。)(2)serviceservice沒有可視化的用戶

8、界而,而是在一段時間內在后臺運行。例如,一個 service可以在用戶做其它事情的時候在后臺播放背景音樂、從網絡上獲取數據或者 計算一些東西并提供給需要這個運算結果的activity使用。每個service都繼承自 service 基類。一個媒體播放器播放播放列表中的曲目是一個不錯的例子。播放器應用程序可 能有一個或多個activity來給用戶選擇歌曲并進行播放。然而,音樂播放這個任務 本身開應該由任何activity來處理,因為用戶的期望即使在他們離開播放器的應用 程序而已經在開始做別的事情時,音樂仍然在繼續播放。為達到這個冃的,媒體播 放器activity可以啟動一個運行于后臺的servi

9、ce服務。系統將在這個activity不再 顯示于屏幕后,仍維持音樂播放的service的運行。連接至(綁定到)一個正在運行的service (如果service沒有運行,則啟動之) 是可能的。連接之后,你可以通過那個service暴露出來的接口不service進行通訊。 對于音樂service來說,這個接口可以允許用戶暫停、回退、停止以及重新開始播 放。如同activity和其它的組件一樣,service服務運行于應用程序進程的主線程內。 所以它不會對其它組件或者用戶界面有任何的妨礙作用,它們一般會派生一個新線 程來執行一些時間消耗型任務(比如音樂回放和網絡下載)。參見稍后的進程和線 程介紹

10、。(3) broadcastreceiverbroadcast receiver是一個與注于接收廣播通知信息,并做出相應處理的組件。 許多廣播是由系統代碼產生的一一例如,通知時區改變、電池電量低、拍攝了一張 照片或者用戶改變了語言選項。應用程序也可以發起廣播一一例如,通知其它應用 程序一些數據已經下載到設備上并處于可用狀態。一個應用程序可以擁有任意數量的broadcast receiver,以對所有它認為重要的 通知信息予以各種響應。所有的receiver均繼承自broadcastreceiver基類。broadcast receiver沒有用戶界而。然而,它們可以啟動一個activity或者

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

12、直接調用返些方法,而是使用一個 contentresolver對彖,調用它的方法作為替代o contentresolver可以與任何content provider進行會話;與其合作對任何相關的進程間通訊進行管理。參閱獨立的content providers文檔以獲得更多關于使用content provider的信 息。每當出現一個需要被特定組件處理的請求時,android會確保那個組件的應用 程序進程處于運行狀態,必要時會啟動它,并確保那個組件的一個合適的實例可用, 必要時會創建那個實例。12激活組件:intent當接收到contentresolver發出的請求后,content provi

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

14、1 通過傳遞一 intentcontext.startactivity()activity.startactivityforresult(以啟動 (或指定新工作給)另外一個activityo相應的activity可以通過調用自身的 getlntent()方法來查看并且激活它的intento android通過調用activity的 onnewintent()方法來傳遞給它隨后的任何intento一個activity經常啟動另一個activityo如果它期望它所啟動的那個activity 返回一個結果,它會調用startactivityforresult()而不是start activity (

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

16、 j間的活動連接。此service會通過onbind()方法的調用來獲取 此intent對象(如果此service尚未運行,bindservice()會先啟動它)。例如,一個 activity可以建立一個不前述的音樂回放service的連接,這樣它就可以提供給用戶 一些途徑(用戶界面)來控制回放。這個activity可以調用bindservice()來建立此 連接,然后調用service中定之的方法來控制回放。稍后的遠程方法調用一節有關于如何綁定至一個service的更多細節。3應用程序可以通過傳遞一個intent對彖至context.sendbroadcast() , context. se

17、ndorderedbroadcast(),以及 context.sendstickybroadcast()和其它類似方法來發起 一個廣播。android會調用所有對此廣播有興趣的broadcast receiver的onreceive() 方法,將此intent傳遞給它們。1.3關閉組件content provider僅在響應來自contentresolver的請求時處于不同活動狀態。而 broadcast receiver僅在響應一條廣播信息的時候處于各種活動狀態。所以沒有必要 去顯式地關閉這組件。而activity則不同,它提供了用戶界而。只要會話依然持續,無論會話過程有 無空閑,acti

18、vity同用戶進行長時間會話且可能一直處于活動狀態。與此相似,service 也會在很長一段時間內在后臺保持運行。所以android為關閉activity和service提 供了一系列有序的方法。activity可以通過調用自身的finish。方法來關閉。一個activity可以通過調用 finishactivity()方法來關閉另夕卜一個 activity (它用 startactivityforresult()啟動的)。service可以通過調用自身的stopself()方法,或調用context.stopservice() 來停止。系統也會在組件不再被使用的時候戒者當android必須為

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

20、程序期望獲得的各種權限。但manifest文件最重要的任務是向android報告此應用程序的各個組件。丼例 說明,一個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屬性指定了實現此activity的activity子類。icon和label 屈性指向包含展示給用戶的此activity的圖標和標簽的資源文件。其它組件也以類似的方法聲明<service>元素用于聲明service,<receiver> 元素用于聲明 broadcast receiver, 而 <provider> 元素用于聲明 content providero 未在 mani

22、fest 文件中進行聲明的 activity> service 以及 content provider 將 不為系統所見,從而也就永不會被運行。然而,broadcast receiver既可以在manifest 文件中聲明,也可以在代碼中動態創建(為broadcastreceiver對彖),并以調用 context.registerreceiver()的方式注冊至系統。1.5 intent過濾器一個intent對象可以顯式地指定一個目標組件。如果進行了返種指定,android 會找到這個組件(基于manifest文件中的聲明)并激活它。但如果intent沒有顯式 地指定一個冃標,andr

23、oid就必須找到最合適的組件來響應此intento這個過程是通 過比較intent對象和所有潛在冃標的intent過濾器完成的。組件的intent過濾器會 通知android它所能處理的intent類型。如同組件的其它必要信息一樣,這些intent 過濾器是在manifest文件中進行聲明的。返里有一個對先前例子的擴展,其中加入 了針對activity的兩個intent過濾器:<?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>示例中的第一個過濾器action ent.action.main n 和category ent.category.launchern 的組合是常見的一個。它標明了此activity應該在應用程序啟動器中顯示,就是用戶在屏幕上看到的此設備上可 供啟動的應用程序的列表。換句話說,這個activity是應用程序的入口點,是用戶 在啟動器中選擇運行這個應用程序后所見到的第一個activityo第二個過濾器聲明了此activity在一種特定類型的數據上可以執行的操作。一個組件可以擁有任意數量的intent過濾器,每

27、個都聲明了一套不同的功能。 如果組件沒有包含任何過濾器,它只能被顯式地指明作為冃標組件的intent激活。對于在代碼中創建并注冊的broadcast receiver來說,intent過濾器將被直接實 例化intentfilter為對象。其它所有的過濾器都在manifest文件中設置。1.6基于xml的布局雖然純粹通過java代碼在activity ±創建和添加部件,在技術上是可行的,我 們在第4章中做的一樣,更常見的方法是使用一種基于xml的布局文件。動態的 小部件實例保留更多,情況復雜,小工具在編譯時不為人所知(例如,在數據檢索 了互聯網基礎上將單選按鈕填充柱。考慮到這一點,現在

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

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

30、還使用視圖定義的xmlo而“隨大流”并不一定是最好的 政策,但他們有優勢幫助從任何其他xml為中心的觀點描述語言輕松進入 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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論