版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、英語(yǔ)原文AndroidApplicationFundamentalsAndroidapplicationsarewrittenintheJavaprogramminglanguage.TheAndroidSDKtoolscompilethecodealongwithanydataandresourcefiles-intoanAndroidpackageanarchivean.apksuffix.Allthecodeinasingle.apkconsideredtobeoneapplicationandistheAndroid-powereddevicesusetoinstalltheappli
2、cation.Onceinstalledonadevice,eachAndroidapplicationlivesinitsownsecuritysandbox:TheAndroidoperatingsystemisamulti-userLinuxsysteminwhicheachapplicationisadifferentuser.Bydefault,thesystemassignseachapplicationauniqueLinuxuserID(theIDisusedonlybythesystemandisunknowntotheapplication).Thesystemsetspe
3、rmissionsforallthefilesinanapplicationsothatonlytheuserIDassignedtothatapplicationcanaccessthem.Eachprocesshasitsownvirtualmachine(VM),soanapplicationscoderunsinisolationfromotherapplications.Bydefault,everyapplicationrunsinitsownLinuxprocess.Androidstartstheprocesswhenanyoftheapplicationscomponents
4、needtobeexecuted,thenshutsdowntheprocesswhenitsnolongerneededorwhenthesystemmustrecovermemoryforotherapplications.Inthisway,theAndroidsystemimplementstheprincipleofleastprivilege.Thatis,eachapplication,bydefault,hasaccessonlytothecomponentsthatitrequirestodoitsworkandnomore.Thiscreatesaverysecureenv
5、ironmentinwhichanapplicationcannotaccesspartsofthesystemforwhichitisnotgivenpermission.However,therearewaysforanapplicationtosharedatawithotherapplicationsandforanapplicationtoaccesssystemservices:ItspossibletoarrangefortwoapplicationstosharethesameLinuxuserID,inwhichcasetheyareabletoaccesseachother
6、sfiles.Toconservesystemresources,applicationswiththesameuserIDcanalsoarrangetoruninthesameLinuxprocessandsharethesameVM(theapplicationsmustalsobesignedwiththesamecertificate).Anapplicationcanrequestpermissiontoaccessdevicedatasuchastheuserscontacts,SMSmessages,themountablestorage(SDcard),camera,Blue
7、tooth,andmore.Allapplicationpermissionsmustbegrantedbytheuseratinstalltime.ThatcoversthebasicsregardinghowanAndroidapplicationexistswithinthesystem.Therestofthisdocumentintroducesyouto:Thecoreframeworkcomponentsthatdefineyourapplication.Themanifestwhichyoudeclarecomponentsandrequireddevicefeaturesfo
8、ryourapplication.Resourcesthatareseparatefromtheapplicationcodeandallowyourapplicationtogracefullyoptimizeitsbehaviorforavarietyofdeviceconfigurations.ApplicationComponentsApplicationcomponentsaretheessentialbuildingblocksofanAndroidapplication.Eachcomponentisadifferentpointthroughwhichthesystemcane
9、nteryourapplication.Notallcomponentsareactualentrypointsfortheuserandsomedependoneachother,buteachoneexistsasitsownentityandplaysaspecificrole-eachoneisauniquebuildingblockthathelpsdefineyourapplicationsoverallbehavior.Therearefourdifferenttypesofapplicationcomponents.Eachtypeservesadistinctpurposea
10、ndhasadistinctlifecyclethatdefineshowthecomponentiscreatedanddestroyed.Herearethefourtypesofapplicationcomponents:ActivitiesAnactivityrepresentsasinglescreenwithauserinterface.Forexample,anemailapplicationmighthaveoneactivitythatshowsalistofnewemails,anotheractivitytocomposeanemail,andanotheractivit
11、yforreadingemails.Althoughtheactivitiesworktogethertoformacohesiveuserexperienceintheemailapplication,eachoneisindependentoftheothers.Assuch,adifferentapplicationcanstartanyoneoftheseactivities(iftheemailapplicationallowsit).Forexample,acameraapplicationcanstarttheactivityintheemailapplicationthatco
12、mposesnewmail,inorderfortheusertoshareapicture.AnactivityisimplementedasasubclassoActivityandyoucanlearnmoreaboutitintheActivitiesdeveloperguide.ServicesAserviceisacomponentthatrunsinthebackgroundtoperformlong-runningoperationsortoperformworkforremoteprocesses.Aservicedoesnotprovideauserinterface.Fo
13、rexample,aservicemightplaymusicinthebackgroundwhiletheuserisinadifferentapplication,oritmightfetchdataoverthenetworkwithoutblockinguserinteractionwithanactivity.Anothercomponent,suchasanactivity,canstarttheserviceandletitrunorbindtoitinordertointeractwithit.AserviceisimplementedasasubclassofServicea
14、ndyoucanlearnmoreaboutitintheServicesdeveloperguide.ContentprovidersAcontentprovidermanagesasharedsetofapplicationdata.Youcanstorethedatainthe,anSQLitedatabase,ontheweb,oranyotherpersistentstoragelocationyourapplicationcanaccess.Throughthecontentprovider,otherapplicationscanqueryorevenmodifythedata(
15、ifthecontentproviderallowsit).Forexample,theAndroidsystemprovidesacontentproviderthatmanagestheuserscontactinformation.Assuch,anyapplicationwiththeproperpermissionscanquerypartofthecontentprovider(suchasContactsContract.Datatoreadandwriteinformationaboutaparticularperson.Contentprovidersarealsousefu
16、lforreadingandwritingdatathatisprivatetoyourapplicationandnotshared.Forexample,theNotePadsampleapplicationusesacontentprovidertosavenotes.AcontentproviderisimplementedasasubclassofContentProviderandmustimplementastandardsetofAPIsthatenableotherapplicationstoperformtransactions.Formoreinformation,see
17、theContentProvidersdeveloperguide.BroadcastreceiversAbroadcastreceiverisacomponentthatrespondstosystem-widebroadcastannouncements.Manybroadcastsoriginatefromthesystemforexample,abroadcastannouncingthatthescreenhasturnedoff,thebatteryislow,orapicturewascaptured.Applicationscanalsoinitiatebroadcasts-f
18、orexample,toletotherapplicationsknowthatsomedatahasbeendownloadedtothedeviceandisavailableforthemtouse.Althoughbroadcastreceiversdontdisplayauserinterface,theymaycreateastatusbarnotificationtoalerttheuserwhenabroadcasteventoccurs.Morecommonly,though,abroadcastreceiverisjustagatewaytoothercomponentsa
19、ndisintendedtodoaveryminimalamountofwork.Forinstance,itmightinitiateaservicetoperformsomeworkbasedontheevent.AbroadcastreceiverisimplementedasasubclassofBroadcastReceiverandeachbroadcastisdeliveredasanIntentobject.Formoreinformation,seetheBroadcastReceiverclass.AuniqueaspectoftheAndroidsystemdesigni
20、sthatanyapplicationcanstartanotherapplicationscomponent.Forexample,ifyouwanttheouseptureaphotowiththedevicecamera,theresprobablyanotherapplicationthatdoesthatandyourapplicationcanuseit,insteadofdevelopinganactivitytocaptureaphotoyourself.Youdontneedtoincorporateorevenlinktothecodefromthecameraapplic
21、ation.Instead,youcansimplystarttheactivityinthecameraapplicationthatcapturesaphoto.Whencomplete,thephotoisevenreturnedtoyourapplicationsoyoucanuseit.Totheuser,itseemsasifthecameraisactuallyapartofyourapplication.Whenthesystemstartsacomponent,itstartstheprocessforthatapplication(ifitsnotalreadyrunnin
22、g)andinstantiatestheclassesneededforthecomponent.Forexample,ifyourapplicationstartstheactivityinthecameraapplicationthatcapturesaphoto,thatactivityrunsintheprocessthatbelongstothecameraapplication,notinyourapplicationsprocess.Therefore,unlikeapplicationsonmostothersystems,Androidapplicationsdonthave
23、asingleentrypoint(theresnomain()function,forexample).Becausethesystemrunseachapplicationinaseparateprocesswitthatrestrictaccesstootherapplications,yourapplicationcannotdirectlyactivateacomponentfromanotherapplication.TheAndroidsystem,however,can.So,toactivateacomponentinanotherapplication,youmustdel
24、iveramessagetothesystemthatspecifiesyountenttostartaparticularcomponent.Thesystemthenactivatesthecomponentforyou.ActivatingComponentsThreeofthefourcomponenttypes-activities,services,andbroadcastreceiversareactivatedbyanasynchronousmessagecalledantent.Intentsbindindividualcomponentstoeachotheratrunti
25、me(youcanthinkofthemasthemessengersthatrequestanactionfromothercomponents),whetherthecomponentbelongstoyourapplicationoranother.AnintentiscreatedwithanIntentobject,whichdefinesamessagetoactivateeitheraspecificcomponentoraspecifictypeofcomponentanintentcanbeeitherexplicitorimplicit,respectively.Forac
26、tivitiesandservices,anintentdefinestheactiontoperform(forexample,tovieworsendsomething)andmayspecifytheURIofthedatatoacton(amongotherthingsthatthecomponentbeingstartedmightneedtoknow).Forexample,anintentmightconveyarequestforanactivitytoshowanimageortoopenawebpage.Insomecases,youcanstartanactivityto
27、receivearesult,inwhichcase,theactivityalsoreturnstheresultinanIntent(forexample,youcanissueanintenttolettheuserpickapersonalcontactandhaveitreturnedtoyouthereturnintentincludesaURIpointingtothechosencontact).Forbroadcastreceivers,theintentsimplydefinestheannouncementbeingbroadcast(forexample,abroadc
28、asttoindicatethedevicebatteryislowincludesonlyaknownactionstringthatindicatesbatteryislow).Theothercomponenttype,contentprovider,isnotactivatedbyintents.Rather,itisactivatedwhentargetedbyarequestfronaContentResolverThecontentresolverhandlesalldirecttransactionswiththecontentprovidersothatthecomponen
29、tthatsperformingtransactionswiththeproviderdoesntneedtoandinsteadcallsmethodsontheContentResolverobject.Thisleavesalayerofabstractionbetweenthecontentproviderandthecomponentrequestinginformation(forsecurity).Thereareseparatemethodsforactivatingeachtypeofcomponent:?Youcanstartanactivity(orgiveitsomet
30、hingnewtodo)bypassinganIntenttostartActivity()orstartActivityForResult()(whenyouwanttheactivitytoreturnaresult).?Youcanstartaservice(orgivenewinstructionstoanongoingservice)bypassinganIntenttostartService().OryoucanbindtotheservicebypassinganIntenttobindService().?Youcaninitiateabroadcastbypassingal
31、ntenttomethodslikesendBroadcast()sendOrderedBroadcast,)orsendStickyBroadcast()?Youcanperformaquerytoacontentproviderbycallingquery()onaContentResolverFormoreinformationaboutusingintents,seethententsandIntentFiltersdocument.Moreinformationaboutactivatingspecificcomponentsisalsoprovidedinthefollowingd
32、ocuments:Activities,ServicesBroadcastReceiverandContentProvidersDeclaringcomponentsTheprimarytaskofthemanifestistoinformthesystemabouttheapplicationscomponents.Forexample,amanifestdeclareanactivityasfollows:/activity.Intheelement,theandroid:iconattributepointstoresourcesforaniconthatidentifiestheapp
33、lication.Intheelement,theandroid:nameattributespecifiesthefUlyqualifiedclassnameoftheActivitysubclassandtheandroid:labelattributesspecifiesastringtouseastheuser-visiblelabelfortheactivity.Youmustdeclareallapplicationcomponentsthisway:elementsforactivitieselementsforserviceselementsforbroadcastreceiv
34、erselementsforcontentprovidersActivities,services,andcontentprovidersthatyouincludeinyoursourcebutdonotdeclareinthemanifestarenotvisibletothesystemand,consequently,canneverrun.However,broadcastreceiverscanbeeitherdeclaredinthemanifestorcreateddynamicallyincode(asBroadcastReceiverobjects)andregistere
35、dwiththesystembycallingregisterReceiver()DeclaringcomponentcapabilitiesAsdiscussedabove,inActivatingComponents,youcanuseanIntenttostartactivities,services,andbroadcastreceivers.Youcandosobyexplicitlynamingthetargetcomponent(usingthecomponentclassname)intheintent.However,therealpowerofintentsliesinth
36、econceptofintentactions.Withintentactions,yousimplydescribethetypeofactionyouwanttoperform(andoptionally,thedatauponwhichyoudliketoperformtheaction)andallcthesystemtofindacomponentonthedevicethatcanperformtheactionandstartit.Iftherearemultiplecomponentsthatcanperformtheactiondescribedbytheintent,the
37、ntheuserselectswhichonetouse.Thewaythesystemidentifiesthecomponentsthatcanrespondtoanintentisbycomparingtheintentreceivedtotheintentfiltersprovidedinthemanifestotherapplicationsonthedevice.Whenyoudeclareacomponentinyourapplicationsmanifest,youcanoptionallyincludeintentfiltersthatdeclarethecapabiliti
38、esofthecomponentsoitcanrespondtointentsfromotherapplications.Youcandeclareanintentilterforyourcomponentbyaddinganelementasachildofthecomponentsdeclarationelement.Forexample,anemailapplicationwithanactivityforcomposinganewemailmightdeclareanintentfilterinitsmanifestentrytorespondtosendintents(inorder
39、tosendemail).Anactivityinyourapplicationcanthencreateanintentwiththe”sene(ACTION_SEND),whichthesystemmatchestotheemailapplicationssend”actilaunchesitwhenyouinvoketheintentwithstartActivity().Formoreaboutcreatingintentfilters,seethententsandIntentFiltersdocument.DeclaringapplicationrequirementsTherea
40、reavarietyofdevicespoweredbyAndroidandnotallofthemprovidethesamefeaturesandcapabilities.Inordertopreventyourapplicationfrombeinginstalledondevicesthatlackfeaturesneededbyyourapplication,itsimportantthatyouclearlydefineaprothetypesofdevicesyourapplicationsupportsbydeclaringdeviceandsoftwarerequiremen
41、tsinyourmanifestfile.Mostofthesedeclarationsareinformationalonlyandthesystemdoesnotreadthem,butexternalservicessuchasGooglePlaydoreadtheminordertoprovidefilteringforuserswhentheysearchforapplicationsfromtheirdevice.Forexample,ifyourapplicationrequiresacameraandusesAPIsintroducedinAndroid2.1(APILevel
42、7),youshoulddeclaretheseasrequirementsinyourmanifestfile.Thatway,devicesthatdonothaveacameraandhaveanAndroidversionowerthan2.1cannotinstallyourapplicationfromGooglePlay.However,youcanalsodeclarethatyourapplicationusesthecamera,butdoesnotrequireit.Inthatcase,yourapplicationmustperformacheckatruntimet
43、odetermineifthedevicehasacameraanddisableanyfeaturesthatusethecameraifoneisnotavailable.Herearesomeoftheimportantdevicecharacteristicsthatyoushouldconsiderasyoudesignanddevelopyourapplication:ScreensizeanddensityInordertocategorizedevicesbytheirscreentype,Androiddefinestwocharacteristicsforeachdevic
44、e:screensize(thephysicaldimensionsofthescreen)andscreendensity(thephysicaldensityofthepixelsonthescreen,ordpi-dotsperinch).Tosimplifyallthedifferenttypesofscreenconfigurations,theAndroidsystemgeneralizesthemintoselectgroupsthatmakethemeasiertotarget.Thescreensizesare:small,normal,large,andextralarge
45、.Thescreendensitiesare:lowdensity,mediumdensity,highdensity,andextrahighdensity.Bydefault,yourapplicationiscompatiblewithallscreensizesanddensities,becausetheAndroidsystemmakestheappropriateadjustmentstoyourUIlayoutandimageresources.However,youshouldcreatespecializedlayoutsforcertainscreensizesandpr
46、ovidespecializedimagesforcertaindensities,usingalternativelayoutresources,andbydeclaringinyourmanifestexactlywhichscreensizesyourapplicationsupportswiththeelement.Formoreinformation,seetheSupportingMultipleScreensdocument.InputconfigurationsManydevicesprovideadifferenttypeofuserinputmechanism,suchas
47、ahardwarekeyboard,atrackball,orafive-waynavigationpad.Ifyourapplicationrequiresaparticularkindofinputhardware,thenyoushoulddeclareitinyourmanifestwiththeelement.However,itisrarethatanapplicationshouldrequireacertaininputconfiguration.DevicefeaturesTherearemanyhardwareandsoftwarefeaturesthatmayormayn
48、otexistonagivenAndroid-powereddevice,suchasacamera,alightsensor,bluetooth,acertainversionofOpenGL,orthefidelityofthetouchscreen.YoushouldneverassumethatacertainfeatureisavailableonallAndroid-powereddevices(otherthantheavailabilityofthestandardAndroidlibrary),soyoushoulddeclareanyfeaturesusedbyyourap
49、plicationwiththeelement.PlatformVersionDifferentAndroid-powereddevicesoftenrundifferentversionsoftheAndroidplatform,suchasAndroid1.6orAndroid2.3.EachsuccessiveversionoftenincludesadditionalAPIsnotavailableinthepreviousversion.InordertoindicatewhichsetofAPIsareavailable,eachplatformversionspecifiesan
50、APILevel(forexample,Android1.0isAPILevel1andAndroid2.3isAPILevel9).IfyouuseanyAPIsthatwereaddedtotheplatformafterversion1.0,youshoulddeclaretheminimumAPILevelinwhichthoseAPIswereintroducedusingtheelement.Itsimportantthatyoudeclareallsuchrequirementsforyourapplication,because,whenyoudistributeyourapp
51、licationonGooglePlay,thestoreusesthesedeclarationstofilterwhichapplicationsareavailableoneachdevice.Assuch,yourapplicationshouldbeavailableonlytodevicesthatmeetallyourapplicationrequirements.FormoreinformationabouthowGooglePlayfiltersapplicationsbasedonthese(andother)requirements,seethiltersonGoogle
52、Playdocument.ApplicationResourcesAnAndroidapplicationiscomposedofmorethanjustcode-itrequiresresourcesthatareseparatefromthesourcecode,suchasimages,audiofiles,andanythingrelatingtothevisualpresentationoftheapplication.Forexample,youshoulddefineanimations,menus,styles,colors,andthelayoutofactivityuser
53、interfaceswithXMLfiles.Usingapplicationresourcesmakesiteasytoupdatevariouscharacteristicsofyourapplicationwithoutmodifyingcodeandbyprovidingsetsofalternativeresources-enablesyoutooptimizeyourapplicationforavarietyofdeviceconfigurations(suchasdifferentlanguagesandscreensizes).Foreveryresourcethatyoui
54、ncludeinyourAndroidproject,theSDKbuildtoolsdefineauniqueintegerID,whichyoucanusetoreferencetheresourcefromyourapplicationcodeorfromotherresourcesdefinedinXML.Forexample,ifyourapplicationcontainsanimagelogo.png(savedintheres/drawable/directory),theSDKtoolsgeneratearesourceIDnamedR.drawable.logQwhichy
55、oucanusetoreferencetheimageandinsertitinyouruserinterface.Oneofthemostimportantaspectsofprovidingresourcesseparatefromyoursourcecodeistheabilityforyoutoprovidealternativeresourcesfordifferentdeviceconfigurations.Forexample,bydefiningUIstringsinXML,youcantranslatethestringsintootherlanguagesandsaveth
56、osestringsinseparatefiles.Then,basedonalanguagealifierthatyouappendtotheresourcedirectorysname(suchares/values-fr/forFrenchstringvalues)andtheuserslanguagesetting,theAndroidsystemappliestheappropriatelanguagestringstoyourUI.Androidsupportsmanydifferentqualifiersforyouralternativeresources.Thequalifi
57、erisashortstringthatyouincludeinthenameofyourresourcedirectoriesinordertodefinethedeviceconfigurationforwhichthoseresourcesshouldbeused.Asanotherexample,youshouldoftencreatedifferentlayoutsforyouractivities,dependingonthedevicesscreenorientationandsize.Forexample,whenthedevicescreenisinportraitorientation(tall),youmightwantalayoutwithbuttonstobevertical,butwhenthescreenisinlandscapeorientation(wide),thebuttonsshouldbealignedhorizontally.Tochangethelayoutdependingontheorientation,youcandefinetwodifferentlayoutsandapplytheappropriatequalifiertoeachlayoutsdirectoryname.Then,thesy
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年蘇人新版八年級(jí)科學(xué)下冊(cè)月考試卷含答案
- 二零二五年度生態(tài)林撫育與保護(hù)項(xiàng)目承包協(xié)議3篇
- 2024電影拍攝導(dǎo)演雇傭合同3篇
- 小學(xué)語(yǔ)文課堂中的文化多樣性教學(xué)案例分享
- 二零二五年度國(guó)際貿(mào)易欺詐風(fēng)險(xiǎn)管理與合同履約監(jiān)督合同3篇
- 二零二五年度物聯(lián)網(wǎng)解決方案合同2篇
- 家庭教育中的時(shí)間管理與家長(zhǎng)自我成長(zhǎng)的關(guān)系
- 2024旅游合同糾紛免責(zé)協(xié)議書范本解決合同糾紛3篇
- 打造高效課堂學(xué)科融合的實(shí)踐與思考
- 二零二五年度電梯購(gòu)銷與節(jié)能改造服務(wù)合同3篇
- 2025年中聯(lián)重科公司發(fā)展戰(zhàn)略和經(jīng)營(yíng)計(jì)劃
- Unit8 Chinese New Year 第一課時(shí)(說(shuō)課稿)-2024-2025學(xué)年譯林版(三起)英語(yǔ)六年級(jí)上冊(cè)
- 半結(jié)構(gòu)化面試題100題
- 服裝廠班組長(zhǎng)培訓(xùn)
- 2024-2030年中國(guó)鋼結(jié)構(gòu)行業(yè)發(fā)展需求及投資規(guī)劃分析報(bào)告版
- 廣東省公立醫(yī)療機(jī)構(gòu)基本醫(yī)療服務(wù)價(jià)格項(xiàng)目修訂表
- 《激光原理及應(yīng)用》全套課件
- 2022年河南省公務(wù)員錄用考試《行測(cè)》真題及答案解析
- 北京市海淀區(qū)2023-2024學(xué)年高三上學(xué)期期末考試+歷史 含答案
- 急診心律失常的治療
- 2024ESC心房顫動(dòng)管理指南解讀
評(píng)論
0/150
提交評(píng)論