Android外文翻译(Android中英文献)

上传人:ya****h 文档编号:143663742 上传时间:2022-08-26 格式:DOCX 页数:17 大小:68.50KB
返回 下载 相关 举报
Android外文翻译(Android中英文献)_第1页
第1页 / 共17页
Android外文翻译(Android中英文献)_第2页
第2页 / 共17页
Android外文翻译(Android中英文献)_第3页
第3页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
本科毕业设计(论文)外文参考文献译文及原文学院信息工程学院专业测控技术与仪器(光机电一体化方向)年级班别2011级(1)班学号学生姓名指导教师2015年6月1应用程序基础11.1应用程序组件11.2 激活组件:intent31.3 关闭组件41.4 manifest文件51.5 Intent过滤器61.6 基于XML的布局71ApplicationFundamentals81.1 ApplicationComponents81.2 Activatingcomponents:intent111.3 Shuttingdowncomponents121.4 Themanifestfile131.5 Intentfilters141.6 XML-BasedLayout151应用程序基础Android应用程序使用Java编程语言开发。aapt工具把编译后的Java代码连同应用程序所需的其他数据和资源文件一起打包到一个Android包文件中,这个文件使用.apk作为扩展名。此文件是分发并安装应用程序到移动设备的载体;是用户下载到他们的设备的文件。单一.apk文件中的所有代码被认为是一个应用程序。从多个角度来看,每个Android应用程序都存在于它自己的世界之中:1默认情况下,每个应用程序均运行于它自己的Linux进程中。当应用程序中的任何代码需要被执行时,Android启动此进程,而当不再需要此进程并且其它应用程序又请求系统资源时,则就关闭了这个进程。2每个进程都有其独有的虚拟机(VM),所以应用程序代码与所有其它应用程序代码是隔离运行的。3默认情况下,每个应用程序均被赋予一个唯一的Linux用户ID,并加以权限设置,使得应用程序的文件仅对此用户及此应用程序可见尽管也有其它的方法使得这些文件同样能为其他应用程序所访问。1.1应用程序组件Android的一个核心的特性就是一个应用程序可以使用其它应用程序的元素(如果那个应用程序允许的话)。例如,如果你的应用程序需要显示一个图片卷动列表,而另一个应用程序已经开发了一个合用的而又允许别的应用程序使用的话,你可以直接调用那个卷动列表来完成工作,而不用自己再开发一个。你的应用程序并没有吸纳或链接其它应用程序的代码。它只是在有需求的时候启动了其它应用程序的那个功能部分。为达到这个目的,系统必须能够在一个应用程序的任何一部分被需要时启动一个此应用程序的进程,并将那个部分的Java对象实例化。因此,不像其它大多数系统上的应用程序,Android应用程序并没有为应用程序提供一个单独的入口点(比如说,没有main()函数),而是为系统提供了可以实例化和运行所需的必备组件。一共有四种组件类型:(1)Activityactivity是为用户操作而展示的可视化用户界面。例如,一个activity可以展示一个菜单项列表供用户选择,戒者显示一些包含说明文字的照片。一个短消息应用程序可以包括一个用于显示要发送消息到的联系人列表的activity,一个给选定的联系人写短信的activity以及翻阅以前的短信或改变设置的其他activity。尽管它们一起组成了一个内聚的用户界面,但其中每个activity都不其它的保持独立。每一个都实现为以Activity类为基类的子类。一个应用程序可以只有一个activity,戒者,如刚才提到的短信应用程序那样,包含很多个。每个activity的作用,以及有多少个activity,当然是取决于应用程序及其设计的。一般情况下,总有一个应用程序被标记为用户在应用程序启动的时候第一个看到的。从一个activity转向另一个靠的是用当前的activity启动下一个。每个activity都被给予一个默认的窗口以进行绘制。一般情况下,这个窗口是满屏的,但它也可以是一个小的位于其它窗口之上的浮动的窗口。一个activity也可以使用附加的窗口例如,一个在activity运行过程中弹出的供用户响应的对话框,这是一个当用户选择了屏幕上特定项目后显示的必要信息的窗口。窗口显示的可视内容是由一系列层次化view构成的,这些view均继承自View基类。每个view均控制着窗口中一块特定的矩形区域。父级view包含并组织其子view的布局。叶节点view(位于层次结构最底端)在它们控制的矩形区域中进行绘制,并对用户直达其区域的操作做出响应。因此,view是activity与用户进行交互的界面。例如,view可以显示一个小图片,并在用户指点它的时候产生动作。Android有一些预置的view供开发者使用包括按钮、文本域、滚动条、菜单项、复选框等等。view的层次结构是由Activity.setContentView()方法放入activity的窗口之中的。contentview是位于层次结构根位置的View对象。(参见独立的用户界面文档以获取关于view及层次结构的更多信息。)(2)Serviceservice没有可视化的用户界面,而是在一段时间内在后台运行。例如,一个service可以在用户做其它事情的时候在后台播放背景音乐、从网络上获取数据或者计算一些东西并提供给需要这个运算结果的activity使用。每个service都继承自Service基类。一个媒体播放器播放播放列表中的曲目是一个不错的例子。播放器应用程序可能有一个或多个activity来给用户选择歌曲并进行播放。然而,音乐播放这个任务本身丌应该由任何activity来处理,因为用户的期望即使在他们离开播放器的应用程序而已经在开始做别的事情时,音乐仍然在继续播放。为达到这个目的,媒体播放器activity可以启动一个运行于后台的service服务。系统将在这个activity不再显示于屏幕后,仍维持音乐播放的service的运行。连接至(绑定到)一个正在运行的service(如果service没有运行,则启动之)是可能的。连接之后,你可以通过那个service暴露出来的接口不service进行通讯。对于音乐service来说,这个接口可以允许用户暂停、回退、停止以及重新开始播放。如同activity和其它的组件一样,service服务运行于应用程序进程的主线程内。所以它不会对其它组件或者用户界面有任何的妨碍作用,它们一般会派生一个新线程来执行一些时间消耗型任务(比如音乐回放和网络下载)。参见稍后的进程和线程介绍。(3) BroadcastReceiverbroadcastreceiver是一个与注于接收广播通知信息,并做出相应处理的组件。许多广播是由系统代码产生的例如,通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以发起广播例如,通知其它应用程序一些数据已经下载到设备上并处于可用状态。一个应用程序可以拥有任意数量的broadcastreceiver,以对所有它认为重要的通知信息予以各种响应。所有的receiver均继承自BroadcastReceiver基类。broadcastreceiver没有用户界面。然而,它们可以启动一个activity或者service来响应它们收到的信息,当然也可以使用NotificationManager来通知用户。通知可以用多种方式来吸引用户的注意力一闪动背光灯、震动设备、播放声音等等。通知一般是在状态栏上放一个持丽的图标,用户可以点击打开它并获取所要消息。(4) Contentprovidercontentprovider将一些特定的应用程序数据供给其它应用程序使用处理。数据可以存储于文件系统、SQLite数据库或其它有意丿的方式。contentprovider继承于ContentProvider基类,实现了一套使得其他应用程序能够检索和存储它所管理的类型数据的标准方法。然而,应用程序并不直接调用返些方法,而是使用一个ContentResolver对象,调用它的方法作为替代。ContentResolver可以与任何contentprovider进行会话;与其合作对任何相关的进程间通讯进行管理。参阅独立的ContentProviders文档以获得更多关于使用contentprovider的信息。每当出现一个需要被特定组件处理的请求时,Android会确保那个组件的应用程序进程处于运行状态,必要时会启动它,并确保那个组件的一个合适的实例可用,必要时会创建那个实例。1.2激活组件:intent当接收到ContentResolver发出的请求后,contentprovider被激活。而其它三种组件activityservice和broadcastreceiver,被一种叫做intent的异步消息所激活。intent是一个保存着消息内容的Intent对象。对于activity和service来说,它指明了所请求的操作名称,并指定了用来操作的数据的URI和其它一些信息。例如,它可以承载一个对一个activity的请求,让它为用户显示一张图片,或者让用户编辑一些文本。而对于broadcastreceiver来说,Intent对象指明了所通报的操作。例如,它可以对所有感兴趣的对象通报照相按钮被按下。对于每种组件来说,激活的方法也是不同的:1通过传递一IntentContext.startActivity()Activity.startActivityForResult(以启动(或指定新工作给)另外一个activity。相应的activity可以通过调用自身的getIntent()方法来查看并且激活它的intent。Android通过调用activity的onNewIntent()方法来传递给它随后的任何intent。一个activity经常启动另一个activity。如果它期望它所启动的那个activity返回一个结果,它会调用startActivityForResult()而不是startActivity()。例如,如果它启动了另外一个activity以使用户挑选一张自己的照片,它也许想知道哪张照片被选中了。其结果将会被封装在一个Intent对象中,并传递给发出调用的activity的onActivityResult()方法。2通过传递一个Intent对象至Context.startService()以启动一个service(或向正在运行的service给出一个新的指令)。Android调用此service的onStart()方法并将Intent对象传递给它。与此类似,一个intent可以被传递给Context.bindService()以建立一个处于调用组件和目标service乀间的活动连接。此service会通过onBind()方法的调用来获取此Intent对象(如果此service尚未运行,bindService()会先启动它)。例如,一个activity可以建立一个不前述的音乐回放service的连接,这样它就可以提供给用户一些途径(用户界面)来控制回放。这个activity可以调用bindService()来建立此连接,然后调用service中定之的方法来控制回放。稍后的远程方法调用一节有关于如何绑定至一个service的更多细节。3应用程序可以通过传递一个Intent对象至Context.sendBroadcast()Context.sendOrderedBroadcast(),以及Context.sendStickyBroadcast()和其它类似方法来发起一个广播。Android会调用所有对此广播有兴趣的broadcastreceiver的onReceive()方法,将此intent传递给它们。1.3 关闭组件contentprovider仅在响应来自ContentResolver的请求时处于不同活动状态。而broadcastreceiver仅在响应一条广播信息的时候处于各种活动状态。所以没有必要去显式地关闭这组件。而activity则不同,它提供了用户界面。只要会话依然持续,无论会话过程有无空闲?activity同用户进行长时间会话且可能一直处于活动状态。与此相似,service也会在很长一段时间内在后台保持运行。所以Android为关闭activity和service提供了一系列有序的方法。activity可以通过调用自身的finish()方法来关闭。一个activity可以通过调用finishActivity()方法来关闭另外一个activity(它用startActivityForResult()启动的)。service可以通过调用自身的stopSelf()方法,或调用Context.stopService()来停止。系统也会在组件不再被使用的时候戒者当Android必须为更多的活动组件回收内存时关闭它。稍后的组件的生命的周期一节,将对这种可能性及结果进行更详细的介绍讨论。1.4 manifest文件当Android启动一个应用程序组件之前,它必须知道那个组件是存在的。因此,应用程序会在一个被打包到Android包中的manifest文件中声明它的组件,.apk文件还将涵括应用程序的代码、文件以及其它资源。manifest文件是一个结构化的XML文件,而且对于所有应用程序,文件名总是AndroidManifest.xml。除了声明此应用程序各个组件,它会做很多其他工作,比如指明应用程序所需链接到的库的名称(除了默认的Android库外)以及标出应用程序期望获得的各种权限。但manifest文件最重要的任务是向Android报告此应用程序的各个组件。丼例说明,一个activity可能声明如下:?xrnlver5ion=1.0encmanifest.Opplicatian.c/manifest*vactivity元素的name属性指定了实现此activity的Activity子类icon和label属性指向包含展示给用户的此activity的图标和标签的资源文件。其它组件也以类似的方法声明vservice元素用于声明service,vreceiver兀素用于声明broadcastreceiver,而vprovider兀素用于声明contentprovider。未在manifest文件中进行声明的activityservice以及contentprovider将不为系统所见,从而也就永不会被运行。然而,broadcastreceiver既可以在manifest文件中声明,也可以在代码中动态创建(为BroadcastReceiver对象),并以调用Context.registerReceiver()的方式注册至系统。1.5 Intent过滤器一个Intent对象可以显式地指定一个目标组件。如果进行了返种指定,Android会找到这个组件(基于manifest文件中的声明)并激活它。但如果intent没有显式地指定一个目标,Android就必须找到最合适的组件来响应此intent。这个过程是通过比较Intent对象和所有潜在目标的intent过滤器完成的。组件的intent过滤器会通知Android它所能处理的intent类型。如同组件的其它必要信息一样,这些intent过滤器是在manifest文件中进行声明的。返里有一个对先前例子的扩展,其中加入了针对activity的两个intent过滤器:mni=1.0encadiig=iJtf-S?onionandroid:name=&ndroidint&nt.action.MAIIM/applicarioip/it日nitest?示例中的第一个过滤器action“android.intent.action.MAIN和category“android.intent.category.LAUNCHER的组合是常见的一个。它标明了此activity应该在应用程序启动器中显示,就是用户在屏幕上看到的此设备上可供启动的应用程序的列表。换句话说,这个activity是应用程序的入口点,是用户在启动器中选择运行这个应用程序后所见到的第一个activity。第二个过滤器声明了此activity在一种特定类型的数据上可以执行的操作。一个组件可以拥有任意数量的intent过滤器,每个都声明了一套不同的功能。如果组件没有包含任何过滤器,它只能被显式地指明作为目标组件的intent激活。对于在代码中创建并注册的broadcastreceiver来说,intent过滤器将被直接实例化IntentFilter为对象。其它所有的过滤器都在manifest文件中设置。1.6基于XML的布局虽然纯粹通过Java代码在activity上创建和添加部件,在技术上是可行的,我们在第4章中做的一样,更常见的方法是使用一种基于XML的布局文件。动态的小部件实例保留更多,情况复杂,小工具在编译时不为人所知(例如,在数据检索了互联网基础上将单选按钮填充柱。考虑到这一点,现在是时候打破XML来学习如何用此种方式来布置Androidactivities。正如其名称所示,一个基于XML的布局是一个关系到每个规格的小部件,和他们的容器(更多关于此内容的在第7章)编码的XML格式。具体来说,Android认为基于XML的布局是资源,因此布局的文件存储在res/在你的Android项目布局目录中。每个XML文件包含一个指定的部件和容器布局元素树,一种意见认为构成层次。对XML元素的属性,描述一个部件应如何看或者一个容器应如何运转。例如,如果一个按钮元素。有一个Android的属性值:文字样式=“bold”这意味着该文本出现在按钮的表面应该是呈现一个粗体字体样式.Android的SDK中附带一个使用的布局的工具(aapt)。这个工具应自动调用你的Android工具链(例如,Eclipse中,Antsbuild.xml)。作为一个开发人员,尤其重要的是,在您的项目中aapt生成R.java源文件,让您能在那些布局中直接从Java代码中获取布局和部件。XML作为一个GUI定义格式是越来越流行普遍。微软的XAML,Adobe的Flex,和Mozilla的XUL都采取Android类似的方法:把布局细节放在一个XML文件和把编程智慧资料放在源文件(例如,XUL中的JavaScript)。许多不太知名的图形用户界面框架,如ZK,还使用视图定义的XML。而“随大流”并不一定是最好的政策,但他们有优势帮助从任何其他XML为中心的观点描述语言轻松进入Android。1ApplicationFundamentalsAndroidapplicationsarewrittenintheJavaprogramminglanguage.ThecompiledJavacodealongwithanydataandresourcefilesrequiredbytheapplicationisbundledbytheaapttoolintoanAndroidpackage,anarchivefilemarkedbyan.apksuffix.Thisfileisthevehiclefordistributingtheapplicationandinstallingitonmobiledevices;itsthefileusersdownloadtotheirdevices.Allthecodeinasingle.apkfileisconsideredtobeoneapplication.Inmanyways,eachAndroidapplicationlivesinitsownworld:1. Bydefault,everyapplicationrunsinitsownLinuxprocess.Androidstartstheprocesswhenanyoftheapplicationscodeneedstobeexecuted,andshutsdowntheprocesswhenitsnolongerneededandsystemresourcesarerequiredbyotherapplications.2. Eachprocesshasitsownvirtualmachine(VM),soapplicationcoderunsinisolationfromthecodeofallotherapplications.3. Bydefault,eachapplicationisassignedauniqueLinuxuserID.Permissionsaresetsothattheapplicationsfilesarevisibleonlytothatuserandonlytotheapplicationitselfalthoughtherearewaystoexportthemtootherapplicationsaswell.ItspossibletoarrangefortwoapplicationstosharethesameuserID,inwhichcasetheywillbeabletoseeeachothersfiles.Toconservesystemresources,applicationswiththesameIDcanalsoarrangetoruninthesameLinuxprocess,sharingthesameVM.1.1ApplicationComponentsAcentralfeatureofAndroidisthatoneapplicationcanmakeuseofelementsofotherapplications(providedthoseapplicationspermitit).Forexample,ifyourapplicationneedstodisplayascrollinglistofimagesandanotherapplicationhasdevelopedasuitablescrollerandmadeitavailabletoothers,youcancalluponthatscrollertodothework,ratherthandevelopyourown.Yourapplicationdoesntincorporatethecodeoftheotherapplicationorlinktoit.Rather,itsimplystartsupthatpieceoftheotherapplicationwhentheneedarises.Forthistowork,thesystemmustbeabletostartanapplicationprocesswhenanypartofitisneeded,andinstantiatetheJavaobjectsforthatpart.Therefore,unlikeapplicationsonmostothersystems,Androidapplicationsdonthaveasingleentrypointforeverythingintheapplication(nomain()function,forexample).Rather,theyhaveessentialcomponentsthatthesystemcaninstantiateandrunasneeded.Therearefourtypesofcomponents:(1) ActivitiesAnactivitypresentsavisualuserinterfaceforonefocusedendeavortheusercanundertake.Forexample,anactivitymightpresentalistofmenuitemsuserscanchoosefromoritmightdisplayphotographsalongwiththeircaptions.Atextmessagingapplicationmighthaveoneactivitythatshowsalistofcontactstosendmessagesto,asecondactivitytowritethemessagetothechosencontact,andotheractivitiestoreviewoldmessagesorchangesettings.Thoughtheyworktogethertoformacohesiveuserinterface,eachactivityisindependentoftheothers.EachoneisimplementedasasubclassoftheActivitybaseclass.Anapplicationmightconsistofjustoneactivityor,likethetextmessagingapplicationjustmentioned,itmaycontainseveral.Whattheactivitiesare,andhowmanytherearedepends,ofcourse,ontheapplicationanditsdesign.Typically,oneoftheactivitiesismarkedasthefirstonethatshouldbepresentedtotheuserwhentheapplicationislaunched.Movingfromoneactivitytoanotherisaccomplishedbyhavingthecurrentactivitystartthenextone.Eachactivityisgivenadefaultwindowtodrawin.Typically,thewindowfillsthescreen,butitmightbesmallerthanthescreenandfloatontopofotherwindows.Anactivitycanalsomakeuseofadditionalwindowsforexample,apop-updialogthatcallsforauserresponseinthemidstoftheactivity,orawindowthatpresentsuserswithvitalinformationwhentheyselectaparticularitemon-screen.ThevisualcontentofthewindowisprovidedbyahierarchyofviewsobjectsderivedfromthebaseViewclass.Eachviewcontrolsaparticularrectangularspacewithinthewindow.Parentviewscontainandorganizethelayoutoftheirchildren.Leafviews(thoseatthebottomofthehierarchy)drawintherectanglestheycontrolandrespondtouseractionsdirectedatthatspace.Thus,viewsarewheretheactivitysinteractionwiththeusertakesplace.Forexample,aviewmightdisplayasmallimageandinitiateanactionwhentheusertapsthatimage.Androidhasanumberofready-madeviewsthatyoucanuseincludingbuttons,textfields,scrollbars,menuitems,checkboxes,andmore.AviewhierarchyisplacedwithinanactivityswindowbytheActivity.setContentView()method.ThecontentviewistheViewobjectattherootofthehierarchy.(SeetheseparateUserInterfacedocumentformoreinformationonviewsandthehierarchy.)(2) ServicesAservicedoesnthaveavisualuserinterface,butratherrunsinthebackgroundforanindefiniteperiodoftime.Forexample,aservicemightplaybackgroundmusicastheuserattendstoothermatters,oritmightfetchdataoverthenetworkorcalculatesomethingandprovidetheresulttoactivitiesthatneedit.EachserviceextendstheServicebaseclass.Aprimeexampleisamediaplayerplayingsongsfromaplaylist.Theplayerapplicationwouldprobablyhaveoneormoreactivitiesthatallowtheusertochoosesongsandstartplayingthem.However,themusicplaybackitselfwouldnotbehandledbyanactivitybecauseuserswillexpectthemusictokeepplayingevenaftertheyleavetheplayerandbeginsomethingdifferent.Tokeepthemusicgoing,themediaplayeractivitycouldstartaservicetoruninthebackground.Thesystemwouldthenkeepthemusicplaybackservicerunningevenaftertheactivitythatstarteditleavesthescreen.Itspossibletoconnectto(bindto)anongoingservice(andstarttheserviceifitsnotalreadyrunning).Whileconnected,youcancommunicatewiththeservicethroughaninterfacethattheserviceexposes.Forthemusicservice,thisinterfacemightallowuserstopause,rewind,stop,andrestarttheplayback.Likeactivitiesandtheothercomponents,servicesruninthemainthreadoftheapplicationprocess.Sothattheywontblockothercomponentsortheuserinterface,theyoftenspawnanotherthreadfortime-consumingtasks(likemusicplayback).SeeProcessesandThreads,later.(3) BroadcastreceiversAbroadcastreceiverisacomponentthatdoesnothingbutreceiveandreacttobroadcastannouncements.Manybroadcastsoriginateinsystemcodeforexample,announcementsthatthetimezonehaschanged,thatthebatteryislow,thatapicturehasbeentaken,orthattheuserchangedalanguagepreference.Applicationscanalsoinitiatebroadcastsforexample,toletotherapplicationsknowthatsomedatahasbeendownloadedtothedeviceandisavailableforthemtouse.Anapplicationcanhaveanynumberofbroadcastreceiverstorespondtoanyannouncementsitconsidersimportant.AllreceiversextendtheBroadcastReceiverbaseclass.Broadcastreceiversdonotdisplayauserinterface.However,theymaystartanactivityinresponsetotheinformationtheyreceive,ortheymayusetheNotificationManagertoalerttheuser.Notificationscangettheusersattentioninvariouswaysflashingthebacklight,vibratingthedevice,playingasound,andsoon.Theytypicallyplaceapersistenticoninthestatusbar,whichuserscanopentogetthemessage.(4) ContentprovidersAcontentprovidermakesaspecificsetoftheapplicationsdataavailabletootherapplications.Thedatacanbestoredinthefilesystem,inanSQLitedatabase,orinanyothermannerthatmakessense.ThecontentproviderextendstheContentProviderbaseclasstoimplementastandardsetofmethodsthatenableotherapplicationstoretrieveandstoredataofthetypeitcontrols.However,applicationsdonotcallthesemethodsdirectly.RathertheyuseaContentResolverobjectandcallitsmethodsinstead.AContentResolvercantalktoanycontentprovider;itcooperateswiththeprovidertomanageanyinterprocesscommunicationthatsinvolved.SeetheseparateContentProvidersdocumentformoreinformationonusingcontentproviders.Whenevertheresarequestthatshouldbehandledbyaparticularcomponent,Androidmakessurethattheapplicationprocessofthecomponentisrunning,startingitifnecessary,andthatanappropriateinstanceofthecomponentisavailable,creatingtheinstanceifnecessary.1.2 Activatingcomponents:intentsContentprovidersareactivatedwhentheyretargetedbyarequestfromaContentResolver.Theotherthreecomponentsactivities,services,andbroadcastreceiversareactivatedbyasynchronousmessagescalledintents.AnintentisanIntentobjectthatholdsthecontentofthemessage.Foractivitiesandservices,itnamestheactionbeingrequestedandspecifiestheURIofthedatatoacton,amongotherthings.Forexample,itmightconveyarequestforanactivitytopresentanimagetotheuserorlettheusereditsometext.Forbroadcastreceivers,theIntentobjectnamestheactionbeingannounced.Forexample,itmightannouncetointerestedpartiesthatthecamerabuttonhasbeenpressed.Thereareseparatemethodsforactivatingeachtypeofcomponent:1. Anactivityislaunched(orgivensomethingnewtodo)bypassinganIntentobjecttoContext.startActivity()orActivity.startActivityForResult().TherespondingactivitycanlookattheinitialintentthatcausedittobelaunchedbycallingitsgetIntent()method.AndroidcallstheactivitysonNewIntent()methodtopassitanysubsequentintents.Oneactivityoftenstartsthenextone.Ifitexpectsaresultbackfromtheactivityitsstarting,itcallsstartActivityForResult()insteadofstartActivity().Forexample,ifitstartsanactivitythatletstheuserpickaphoto,itmightexpecttobereturnedthechosenphoto.TheresultisreturnedinanIntentobjectthatspassedtothecallingactivitysonActivityResult()method.2. Aserviceisstarted(ornewinstructionsaregiventoanongoingservice)bypassinganIntentobjecttoContext.startService().AndroidcallstheservicesonStart()methodandpassesittheIntentobject.Similarly,anintentcanbepassedtoContext.bindService()toestablishanongoingconnectionbetweenthecallingcomponentandatargetservice.TheservicereceivestheIntentobjectinanonBind()call.(Iftheserviceisnotalreadyrunning,bindService()canoptionallystartit.)Forexample,anactivitymightestablishaconnectionwiththemusicplaybackservicementionedearliersothatitcanprovidetheuserwiththemeans(auserinterface)forcontrollingtheplayback.TheactivitywouldcallbindService()tosetupthatconnection,andthencallmethodsdefinedbytheservicetoaffecttheplayback.Alatersection,Remoteprocedurecalls,hasmoredetailsaboutbindingtoaservice.3. AnapplicationcaninitiateabroadcastbypassinganIntentobjecttomethodslikeContext.sendBroadcast(),Context.sendOrderedBroadcast(),andContext.sendStickyBroadcast()inanyoftheirvariations.AndroiddeliverstheintenttoallinterestedbroadcastreceiversbycallingtheironReceive()methods.Formoreonintentmessages,seetheseparatearticle,IntentsandIntentFilters.1.3 ShuttingdowncomponentsAcontentproviderisactiveonlywhileitsrespondingtoarequestfromaContentResolver.Andabroadcastreceiverisactiveonlywhileitsrespondingtoabroadcastmessage.Sotheresnoneedtoexplicitlyshutdownthesecomponents.Activities,ontheotherhand,providetheuserinterface.Theyreinalong-runningconversationwiththeuserandmayremainactive,evenwhenidle,aslongastheconversationcontinues.Similarly,servicesmayalsoremainrunningforalongtime.SoAndroidhasmethodstoshutdownactivitiesandservicesinanorderlyway:1.Anactivitycanbeshutdownbycallingitsfinish()method.Oneactivitycanshutdownanotheractivity(oneitstartedwithstartActivityForResult()bycallingfinishActivity().2.AservicecanbestoppedbycallingitsstopSelf()method,orbycallingContext.stopService().ComponentsmightalsobeshutdownbythesystemwhentheyarenolongerbeingusedorwhenAndroidmustreclaimmemoryformoreactivecomponents.Alatersection,ComponentLifecycles,discussesthispossibilityanditsramificationsinmoredetail.1.4 ThemanifestfileBeforeAndroidcanstartanapplicationcomponent,itmustlearnthatthecomponentexists.Therefore,applicationsdeclaretheircomponentsinamanifestfilethatsbundledintotheAndroidpackage,the.apkfilethatalsoholdstheapplicationscode,files,andresources.ThemanifestisastructuredXMLfileandisalwaysnamedAndroidManifest.xmlforallapplications.Itdoesanumberofthingsinadditiontodeclaringtheapplicationscomponents,suchasnaminganylibrariestheapplicationneedstobelinkedagainst(besidesthedefaultAndroidlibrary)andidentifyinganypermissionstheapplicationexpectstobegranted.ButtheprincipaltaskofthemanifestistoinformAndroidabouttheapplicationscomponents.Forexample,anactivitymightbedeclaredasfol
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业管理 > 商业计划


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!