




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、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 package, an archive file marked by an .apk suffix. this file is the vehicle
2、 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 application lives in its own world:1. by default, every application runs in its own l
3、inux process. android starts the process when any of the applications 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 process has its own virtual machine (vm), so application code runs in isolation from t
4、he code of all other applications.3. by default, each application is assigned a unique linux user id. permissions are set so that the applications files are visible only to that user and only to the application itself although there are ways to export them to other applications as well.its possible
5、to arrange for two applications to share the same user id, in which case they will be able to see each others files. to conserve system resources, applications with the same id can also arrange to run in the same linux process, sharing the same vm.application componentsa central feature of android i
6、s 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 application has developed a suitable scroller and made it available to others, you can call upon tha
7、t scroller to do the work, rather than develop your own. your application doesnt 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
8、 when any part of it is needed, and instantiate the java objects for that part. therefore, unlike applications on most other systems, android applications dont have a single entry point for everything in the application (no main() function, for example). rather, they have essential components that t
9、he system can instantiate and run as needed. there are four types of components:activitiesan activity presents a visual user interface for one focused endeavor the user can undertake. for example, an activity might present a list of menu items users can choose from or it might display photographs al
10、ong 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 contact, and other activities to review old messages or change settings. though they work together to form a cohesive
11、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 text messaging application just mentioned, it may contain several. what the activities are, and how many there a
12、re 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. moving from one activity to another is accomplished by having the current activity start the next one.each acti
13、vity 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 additional windows for example, a pop-up dialog that calls for a user response in the midst of the activity,
14、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 from the base view class. each view controls a particular rectangular space within the window. parent views contain
15、 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. thus, views are where the activitys interaction with the user takes place.for example, a view might display a small im
16、age 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, check boxes, and more.a view hierarchy is placed within an activitys window by the activity.setcontentview() method. the conten
17、t 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.)servicesa service doesnt have a visual user interface, but rather runs in the background for an indefinite period of time. for example, a service might p
18、lay 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 applic
19、ation 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 k
20、eep 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.its possible to connect to (bind to) an ongoing service (and start the service if its
21、 not already running). while connected, you can communicate 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 components, services run in the main thre
22、ad of the application process. so that they wont block other components or the user interface, they often spawn another thread for time-consuming tasks (like music playback). see processes and threads, later.broadcast receiversa broadcast receiver is a component that does nothing but receive and rea
23、ct to broadcast announcements. many broadcasts originate in system code for example, announcements 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 othe
24、r 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 broadcast receivers to respond to any announcements it considers important. all receivers extend the broadcastreceiver base class.broadcast receivers do not dis
25、play a user interface. however, they may start an activity in response to the information they receive, or they may use the notificationmanager to alert the user. notifications can get the users attention in various ways flashing the backlight, vibrating the device, playing a sound, and so on. they
26、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 applications data available to other applications. the data can be stored in the file system, in an sqlite database, or in any other manner tha
27、t makes sense. the content provider extends the contentprovider base class to implement a standard set 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
28、call its methods instead. a contentresolver can talk to any content provider; it cooperates with the provider to manage any interprocess communication thats involved.see the separate content providers document for more information on using content providers.whenever theres a request that should be h
29、andled by a particular component, android makes 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.activating components: intentscontent providers are activated wh
30、en theyre 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 actio
31、n being requested and specifies the uri of the data to act on, among other things. for example, it might convey a request for an activity to present an image to the user or let the user edit some text. for broadcast receivers, theintent object names the action being announced. for example, it might
32、announce to interested parties that the camera button has been pressed.there are separate methods for activating each type of component:1. an activity is launched (or given something new to do) by passing an intent object tocontext.startactivity() or activity.startactivityforresult(). the responding
33、 activity can look at the initial intent that caused it to be launched by calling its getintent() method. android calls the activitys onnewintent() method to pass it any subsequent intents. one activity often starts the next one. if it expects a result back from the activity its starting, it calls s
34、tartactivityforresult() instead of startactivity(). for example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. the result is returned in an intent object thats passed to the calling activitys onactivityresult() method.2. a service is start
35、ed (or new instructions are given to an ongoing service) by passing an intent object to context.startservice(). android calls the services onstart() method and passes it the intent object. similarly, an intent can be passed to context.bindservice() to establish an ongoing connection between the call
36、ing 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.) for example, an activity might establish a connection with the music playback service mentioned earlier so that it can provi
37、de 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
38、application can initiate a broadcast by passing an intent object to methods like context.sendbroadcast(), context.sendorderedbroadcast(), and context.sendstickybroadcast() in any of their variations.android delivers the intent to all interested broadcast receivers by calling their onreceive() method
39、s. for more on intent messages, see the separate article, intents and intent filters.shutting down componentsa content provider is active only while its responding to a request from a contentresolver. and a broadcast receiver is active only while its responding to a broadcast message. so theres no n
40、eed to explicitly shut down these components.activities, on the other hand, provide the user interface. theyre 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 remain running for a long time. so an
41、droid 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 st
42、opself() method, or by calling context.stopservice().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 this possibility and its ramifications in more det
43、ail.the manifest filebefore android can start an application component, it must learn that the component exists. therefore, applications declare their components in a manifest file thats bundled into the android package, the .apk file that also holds the applications code, files, and resources.the m
44、anifest is a structured xml file and is always named androidmanifest.xml for all applications. it does a number of things in addition to declaring the applications components, such as naming any libraries the application needs to be linked against (besides the default android library) and identifyin
45、g any permissions the application expects to be granted.but the principal task of the manifest is to inform android about the applications components. for example, an activity might be declared as follows:the name attribute of the element names the activity subclass that implements the activity. the
46、 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 components are declared in a similar way elements for services, elements for broadcast receivers, and elements for content providers. activities, service
47、s, 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 b
48、y calling context.registerreceiver().for more on how to structure a manifest file for your application, see the android manifest.xml file.intent filtersan intent object can explicitly name a target component. if it does, android finds that component (based on the declarations in the manifest file) a
49、nd activates it. but if a target is not explicitly named, android must locate the best component to respond to the intent. it does so by comparing the intent object to the intent filters of potential targets. a components intent filters inform android of the kinds of intents the component is able to
50、 handle. like other essential information about the component, theyre declared in the manifest file. heres an extension of the previous example that adds two intent filters to the activity:the first filter in the example the combination of the action ent.action.main and the categoryandroi
51、ent.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, the activity is the entry point for the application, the initial one users would see when t
52、hey choose the application in the launcher.the second filter declares an action that the activity can perform on a particular type of data.a component can have any number of intent filters, each one declaring a different set of capabilities. if it doesnt have any filters, it can be activated only by
53、 intents that explicitly name the component as the target.for a broadcast receiver thats created and registered in code, the intent filter is instantiated directly as an intentfilter object. all other filters are set up in the manifest.for more on intent filters, see a separate document, intents and
54、 intent filters.應(yīng)用程序基礎(chǔ)android developersandroid應(yīng)用程序使用java編程語言開發(fā)。aapt工具把編譯后的java代碼連同應(yīng)用程序所需的其他數(shù)據(jù)和資源文件一起打包到一個(gè)android包文件中,這個(gè)文件使用.apk作為擴(kuò)展名。此文件是分發(fā)并安裝應(yīng)用程序到移動設(shè)備的載體;是用戶下載到他們的設(shè)備的文件。單一.apk文件中的所有代碼被認(rèn)為是一個(gè)應(yīng)用程序。從多個(gè)角度來看,每個(gè)android應(yīng)用程序都存在于它自己的世界之中: 1 默認(rèn)情況下,每個(gè)應(yīng)用程序均運(yùn)行于它自己的linux進(jìn)程中。當(dāng)應(yīng)用程序中的任何代碼需要被執(zhí)行時(shí),android啟動此進(jìn)程,而當(dāng)不再需要此
55、進(jìn)程并且其它應(yī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)用程序的文件僅對此用戶及此應(yīng)用程序可見盡管也有其它的方法使得這些文件同樣能為其他應(yīng)用程序所訪問。 1 應(yīng)用程序組件android的一個(gè)核心特性就是一個(gè)應(yīng)用程序可以使用其它應(yīng)用程序的元素(如果那個(gè)應(yīng)用程序允許的話)。例如,如果你的應(yīng)用程序需要顯示一個(gè)圖片卷動列表,而另一個(gè)應(yīng)用程序已經(jīng)開發(fā)了一個(gè)合用的而又允許別的應(yīng)用程序使用的話,你可以直接調(diào)用那個(gè)卷動列表來完成工
56、作,而不用自己再開發(fā)一個(gè)。你的應(yīng)用程序并沒有吸納或鏈接其它應(yīng)用程序的代碼。它只是在有需求的時(shí)候啟動了其它應(yīng)用程序的那個(gè)功能部分。 為達(dá)到這個(gè)目的,系統(tǒng)必須能夠在一個(gè)應(yīng)用程序的任何一部分被需要時(shí)啟動一個(gè)此應(yīng)用程序的進(jìn)程,并將那個(gè)部分的java對象實(shí)例化。因此,不像其它大多數(shù)系統(tǒng)上的應(yīng)用程序,android應(yīng)用程序并沒有為應(yīng)用程序提供一個(gè)單獨(dú)的入口點(diǎn)(比如說,沒有main()函數(shù)),而是為系統(tǒng)提供了可以實(shí)例化和運(yùn)行所需的必備組件。一共有四種組件類型: 1 activity activity是為用戶操作而展示的可視化用戶界面。例如,一個(gè)activity可以展示一個(gè)菜單項(xiàng)列表供用戶選擇,戒者顯示一些包
57、含說明文字的照片。一個(gè)短消息應(yīng)用程序可以包括一個(gè)用于顯示要發(fā)送消息到的聯(lián)系人列表的activity,一個(gè)給選定的聯(lián)系人寫短信的activity以及翻閱以前的短信或改變設(shè)置的其他activity。盡管它們一起組成了一個(gè)內(nèi)聚的用戶界面,但其中每個(gè)activity都不其它的保持獨(dú)立。每一個(gè)都實(shí)現(xiàn)為以activity類為基類的子類。 一個(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)用程序啟動的時(shí)候第一個(gè)看到的。從一個(gè)activi
58、ty轉(zhuǎn)向另一個(gè)靠的是用當(dāng)前的activity啟動下一個(gè)。 每個(gè)activity都被給予一個(gè)默認(rèn)的窗口以進(jìn)行繪制。一般情況下,這個(gè)窗口是滿屏的,但它也可以是一個(gè)小的位于其它窗口之上的浮動窗口。一個(gè)activity也可以使用附加窗口例如,一個(gè)在activity運(yùn)行過程中彈出的供用戶響應(yīng)的對話框,戒是一個(gè)當(dāng)用戶選擇了屏幕上特定項(xiàng)目后顯示的必要信息的窗口。 窗口顯示的可視內(nèi)容是由一系列層次化view構(gòu)成的,這些view均繼承自 view 基類。每個(gè)view均控制著窗口中一塊特定的矩形區(qū)域。父級view包含并組織其子view的布局。葉節(jié)點(diǎn)view(位于層次結(jié)構(gòu)最底端)在它們控制的矩形區(qū)域中進(jìn)行繪制,并對用戶直達(dá)其區(qū)域的操作做出響應(yīng)。因此,view是activity與用戶進(jìn)行交互的界面。例如,view可以顯示一個(gè)小圖片,并在用戶指點(diǎn)它的時(shí)候產(chǎn)生動作。android有一些預(yù)置的view供開發(fā)者使用包括按鈕、文本域、滾動條、菜單項(xiàng)、復(fù)選框等等。 view層次結(jié)構(gòu)是由activity.setcontentview() 方法放入activity的窗口之中的。content view是位于層次結(jié)構(gòu)根位置的view對象。(參見獨(dú)立的用戶界面文檔以獲取關(guān)于view及層次結(jié)構(gòu)的更多信息。) 2 service serv
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)村出售地皮合同范本
- 出口定金合同范本
- 業(yè)務(wù)用車租賃合同范本
- 入股果園合同范例
- 第五單元第14課文藝復(fù)興運(yùn)動2023-2024學(xué)年九年級上冊歷史同步教學(xué)設(shè)計(jì)(部編版)
- 專利實(shí)施使用合同范本
- epc項(xiàng)目銷售合同范本
- 2024年溫州龍港農(nóng)商銀行招聘筆試真題
- 借條合同范本范文
- 保安顧問合同范本
- 新風(fēng)施工合同
- 2025-2030年園藝修剪機(jī)器人行業(yè)深度調(diào)研及發(fā)展戰(zhàn)略咨詢報(bào)告
- 人教版四年級數(shù)學(xué)下冊第四單元測試卷(含答案)
- 2025年湖北省技能高考(建筑技術(shù)類)《建筑工程測量》模擬練習(xí)試題庫(含答案)
- 2024-2027年中國網(wǎng)絡(luò)安全評估行業(yè)發(fā)展監(jiān)測及投資戰(zhàn)略研究報(bào)告
- 失智老年人照護(hù)X證書制度試點(diǎn)工作養(yǎng)老護(hù)理職業(yè)和失智老人照護(hù)員工種的發(fā)展講解
- 2025年湖南食品藥品職業(yè)學(xué)院高職單招職業(yè)技能測試近5年??及鎱⒖碱}庫含答案解析
- 企業(yè)數(shù)字化轉(zhuǎn)型戰(zhàn)略-深度研究
- 新種子法律法規(guī)培訓(xùn)講解
- 2025年東營科技職業(yè)學(xué)院高職單招數(shù)學(xué)歷年(2016-2024)頻考點(diǎn)試題含答案解析
- 2025-2030年中國民用通信天線行業(yè)發(fā)展趨勢規(guī)劃研究報(bào)告
評論
0/150
提交評論