Você está na página 1de 9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

Cheat Sheet

Android Application Development All-in-One For Dummies


From Android Application Development All-in-One For Dummies by Barry Burd
Android is an open source, Linux- and Java-based software framework for mobile and portable devices. The Android operating system now powers nearly half of all smartphones in the world today. Android not only has a majority of users, but also has a well-designed Java-based SDK to make developing apps straightforward and fun. This cheat sheet concentrates on commonly used code handy for any developer wishing to try their hand at building their own Android applications.

Commonly Used Code in Android Application Development


"My app needs a broadcast receiver. Can someone remind me how I can code a broadcast receiver? And while you're at it, how does an activity return a result? Oh, heck. Where can I find all that stuff quickly?" Having examples of the kinds of code used in Android application development ready-to-hand can be a big help. You find plenty of examples right here.

Activities
Here's an activity that starts another activity: pbi casCetheAtvt etnsAtvt ulc ls haSetciiy xed ciiy ipeet OCikitnr{ mlmns nlcLsee Bto bto1 utn utn; sai fnlSrn M_CIN="o.lmcd.cin; ttc ia tig YATO cmalyoeato" sai fnlSrn M_R ttc ia tig YUI ="yshm:yshm_pcfcpr" m_ceem_ceeseii_at; @vrie Oerd pbi vi oCet(udesvdntnett){ ulc od nraeBnl aeIsacSae sproCet(aeIsacSae; ue.nraesvdntnett)
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 1/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

stotnVe(.aotmi) eCnetiwRlyu.an; bto1=(utn fnVeBI(.dbto1; utn Bto) idiwydRi.utn) bto1stnlcLsee(hs; utn.eOCikitnrti) } pbi vi oCikVe ag){ ulc od nlc(iw r0 Itn itn =nwItn(; net net e net) itn.eAto(YATO) netstcinM_CIN; itn.eDt(r.as(YUI) netstaaUipreM_R); satciiyitn) trAtvt(net; } } And don't forget when you create a new activity class, you must add a corresponding < c i i y element to your A d o d a i e t x lfile. atvt> nriMnfs.m The O h r c i i yelement's intent filter looks something like this: teAtvt <netfle> itn-itr <cinadodnm=cmalyoeato"/ ato nri:ae"o.lmcd.cin > <aeoy ctgr adodnm=adoditn.aeoyDFUT / nri:ae"nri.netctgr.EAL" > <aaadodshm=m_cee / dt nri:cee"yshm" > <itn-itr /netfle> To get a result from an activity, add the following code (or something resembling it) to your app: fnlitCD_UBR=4; ia n OENME 2 fnlSrn CASAE="haSetciiy; ia tig LSNM CetheAtvt" pbi vi oCikVe ag){ ulc od nlc(iw r0 Itn itn =nwItn(; net net e net) itn.eAto(YATO) netstcinM_CIN; itn.eDt(r.as(YUI) netstaaUipreM_R); satciiyoRsl(net CD_UBR; trAtvtFreutitn, OENME) } poetdvi oAtvtRsl rtce od nciiyeut (n cdNme,itrslCd,Itn itn){ it oeubr n eutoe net net i (eutoe= RSL_K { f rslCd = EUTO) i (oeubr= CD_UBR { f cdNme = OENME) LgiCASAE o.(LSNM, itn.eDt(.eShmSeiiPr() netgtaa)gtceepcfcat); } } }
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 2/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

And in the activity that creates the result, add code of the following kind: Itn itn =nwItn(; net net e net) itn.eDt(r.as(rsl:el") netstaaUipre"euthlo); steutRSL_K itn) eRsl(EUTO, net; fns(; iih)

Services
A service typically does its work without bothering (or even notifying) the user. For example, a stock price service might reach out to the web and get the latest prices of the user's favorite picks. Another app's activity might get data from the service and display the data on the device's screen. The following code is a complete (but not very accurate) weather service: pbi casMWahrevc etnsSrie{ ulc ls yeteSrie xed evc Msegrmsegrolet=nl; esne esneTCin ul MIcmnHnlrmIcmnHnlr= ynoigade ynoigade nwMIcmnHnlr) e ynoigade(; Msegrmsegroevc = esne esneTSrie nwMsegrmIcmnHnlr; e esne(ynoigade) @vrie Oerd pbi IidroBn(netitn){ ulc Bne nidItn net rtr msegroevc.eBne(; eun esneTSriegtidr) } casMIcmnHnlretnsHnlr{ ls ynoigade xed ade @vrie Oerd pbi vi hnlMsaeMsaeicmnMsae { ulc od adeesg(esg noigesg) msegrolet=icmnMsaerpyo esneTCin noigesg.elT; Bnl rpy=nwBnl(; ude el e ude) rpypttig"ete" "tsdr a ngt"; el.uSrn(wahr, I' ak t ih.) Msaerpyesg =Msaeoti(; esg elMsae esg.ban) rpyesg.eDt(el) elMsaestaarpy; ty{ r msegroletsn(elMsae; esneTCin.edrpyesg) }cth(eoexeto e { ac RmtEcpin ) epittcTae) .rnSakrc(; } } } }
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 3/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

In another package, you put the code to call the weather service. Here's some sample code: pbi casSrieosmrciiyetnsAtvt ulc ls evcCnueAtvt xed ciiy ipeet OCikitnr{ mlmns nlcLsee Msegrmsegroevc =nl; esne esneTSrie ul MIcmnHnlrmIcmnHnlr= ynoigade ynoigade nwMIcmnHnlr) e ynoigade(; Msegrmsegrrmevc = esne esneFoSrie nwMsegrmIcmnHnlr; e esne(ynoigade) Srieoncincneto = evcCneto oncin nwMSrieoncin) e yevcCneto(; SaePeeecspes hrdrfrne rf; boeniBud=fle ola son as; vi bn( { od id) Itn itn =nwItn(; net net e net) itn.eAto(cmalyoeWAHR) netstcin"o.lmcd.ETE"; iBud= son bnSrieitn,cneto, idevc(net oncin CnetBN_UOCET) otx.IDAT_RAE; } pbi vi qeyevc( { ulc od urSrie) i (son){ f iBud Bnl bnl =nwBnl(; ude ude e ude) bnl.uSrn(lcto" "hldlha) udepttig"oain, Piaepi"; Msaemsae=Msaeoti(; esg esg esg.ban) msaerpyo=msegrrmevc; esg.elT esneFoSrie msaestaabnl) esg.eDt(ude; ty{ r msegroevc.edmsae; esneTSriesn(esg) }cth(eoexeto e { ac RmtEcpin ) epittcTae) .rnSakrc(; } }es { le txVe1stetRsrn.evc_o_on) etiw.eTx(.tigsrientbud; } } casMIcmnHnlretnsHnlr{ ls ynoigade xed ade
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 4/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

@vrie Oerd pbi vi hnlMsaeMsaemg { ulc od adeesg(esg s) Bnl bnl =mggtaa) ude ude s.eDt(; txVe1stetbnl.eSrn(wahr); etiw.eTx(udegttig"ete") } } vi ubn( { od nid) i (son){ f iBud ubnSriecneto) nidevc(oncin; iBud=fle son as; } } casMSrieoncinipeet Srieoncin{ ls yevcCneto mlmns evcCneto pbi vi oSrieonce( ulc od nevcCnetd Cmoetaecasae Iidrbne){ opnnNm lsNm, Bne idr msegroevc =nwMsegrbne) esneTSrie e esne(idr; } pbi vi oSrieicnetdCmoetaen { ulc od nevcDsonce(opnnNm ) msegroevc =nl; esneTSrie ul } } / IdnticueteoCet mto o te / o' nld h nrae ehd r h / / oCikmto i ti eape nlc ehd n hs xml. } Of course, no app survives without some elements in the manifest file. To register this section's service, you need an element of the following kind: <evc adodnm=.yeteSrie> srie nri:ae"MWahrevc" <netfle> itn-itr <cinadodnm=cmalyoeWAHR / ato nri:ae"o.lmcd.ETE" > <itn-itr /netfle> <srie /evc>

Broadcast receivers
When you do a broadcast, you fling an intent out into the wild. Broadcast receivers with compatible intent filters wake up and do something useful with the broadcast information. (After doing something with the broadcast info, the receiver goes back to sleep. In my next incarnation, I want to be a broadcast receiver.) To create your own broadcast receiver, you extend Android's B o d a t e e v rclass and you declare an o R c i emethod. For racsRcie neev
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 5/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

example, the following code responds to matching broadcasts: pbi casMRcie etnsBodateevr{ ulc ls yeevr xed racsRcie @vrie Oerd pbi vi oRcieCnetcnet Itn itn){ ulc od neev(otx otx, net net / D ipratsuf / o motn tf } } Another app creates a broadcast with code of the following sort: Itn itn =nwItn(; net net e net) itn.eAto(cmalyoeATO"; netstcin"o.lmcd.CIN) snBodatitn) edracs(net; You can register a broadcast receiver in your A d o d a i e t x lfile: nriMnfs.m <eevradodnm=.yeevr> rcie nri:ae"MRcie" <netfle> itn-itr <cinadodnm=cmalyoeATO"/ ato nri:ae"o.lmcd.CIN > <itn-itr /netfle> <rcie> /eevr For more flexibility, you can register a receiver in your Java code. The following Java code does essentially what the < e e v r element in an rcie> A d o d a i e t x lfile does: nriMnfs.m ItnFle fle =nwItnFle(; netitr itr e netitr) fle.dAto(cmalyoeATO"; itradcin"o.lmcd.CIN) rgseRcie(e MRcie(,fle) eitreevrnw yeevr) itr;

Content providers
An app's content provider makes data available to other apps that run on the same device. The provider's interface resembles a database's interface, with tables, rows, cursors, and all that good stuff. For example, the code to query a content provider looks like this: pbi Cro qeyUiui Srn[ clms ulc usr ur(r r, tig] oun, Srn weelue Srn[ weers tig hrCas, tig] hrAg, Srn srOdr { tig otre) Cro cro =nl; usr usr ul itcd =uiace.ac(r) n oe rMthrmthui; i (oe= 1 { f cd = ) cro = usr d.ur(IPEAL,clms weelue bqeySMLTBE oun, hrCas, weers nl,nl,srOdr; hrAg, ul ul otre) }es i (oe= 2 { le f cd = )
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 6/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

Srn[ clmNms={"i" "ae,"mut } tig] ounae _d, nm" aon" ; Srn[ rwaus={"al " " " ""} tig] oVle Tbe , 4 , 2 ; MtiCro mtiCro = arxusr arxusr nwMtiCro(ounae) e arxusrclmNms; mtiCro.dRwrwaus; arxusrado(oVle) cro =mtiCro; usr arxusr } rtr cro; eun usr }

Fragments
A fragment is like a view a visible thing that you can display inside an activity. But unlike a view, a fragment has its own lifecycle methods. So Android can create a little stack of fragments inside an activity. When the user presses the Back button, Android pops a fragment off of the stack. (If there are no fragments to pop, Android pops the entire activity off the task stack.) You can put a fragment into the following frame layout: <rmLyu adodi=@i/os Faeaot nri:d"+ddc" adodlyu_egt"ac_aet nri:aothih=mthprn" adodlyu_it=0x nri:aotwdh"p" adodlyu_egt"" nri:aotwih=1 adodbcgon= nri:akrud "adodat/ealEeetakrud / ?nri:trdtislmnBcgon" > To put a fragment into the layout, you perform a fragment transaction. Here's what a fragment transaction looks like: DcFamn dcFamn =DcFamn.eIsac(ne) osrget osrget osrgetnwntneidx; FamnMngrfamnMngr=gtrgetaae(; rgetaae rgetaae eFamnMngr) FamnTascinfamnTascin= rgetrnato rgetrnato famnMngrbgnrnato(; rgetaae.eiTascin) famnTascinrpaeRi.os dcFamn) rgetrnato.elc(.ddc, osrget; famnTascinadoaktc(ul; rgetrnato.dTBcSaknl) famnTascincmi(; rgetrnato.omt)

Intents and Intent Filters


When an intent meets the right intent filter, the result is a match made in heaven. But the rules for matching intents with filters are complicated. The rules read like the legal clauses in a prenuptial agreement. You can use Java methods to describe an intent. Here are some often-used methods: setAction: Sets the intent's action. (An intent can have only one action.) addCategory: Adds a category to the intent. (An intent can have many categories.)
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 7/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

setData: Sets the intent's URI, and removes the intent's MIME type (if the intent has a MIME type). setType: Sets the intent's MIME type and removes the intent's URI (if the intent has a URI). setDataAndType: Sets both the intent's URI and the intent's MIME type. According to the docs, "This method should very rarely be used." You can also use XML code to describe an intent. <cinadodnm=srn"/ ato nri:ae"tig > <aeoyadodnm=srn"/ ctgr nri:ae"tig > <aaadodshm=srn" dt nri:cee"tig adodhs=srn" nri:ot"tig adodpr=srn" nri:ot"tig adodpt=srn" nri:ah"tig adodptPten"tig nri:ahatr=srn" adodptPei=srn" nri:ahrfx"tig adodmmTp=srn"/ nri:ieye"tig > In the URI h t : / w . l m c d . o : 0 a d o d the scheme is tp/wwalyoecm8/nri, h t , the host is w w a l y o e c m the port is 8 , and the path is tp w.lmcd.o, 0 a d o d The authority (which isn't one of the attributes in a < a a element, nri. dt> but is useful to know about) is w w a l y o e c m 8 . w.lmcd.o:0 You typically set an intent filter's values in the A d o d a i e t x lfile. But nriMnfs.m in Java code, the a d o d c n e t I t n F l e class has lots of nri.otn.netitr useful methods. Here are a few of them: addAction: Adds an action to the filter. addCategory: Adds a category to the filter. addDataScheme: Adds a scheme to the filter. addDataAuthority: Adds an authority to the filter. addDataPath: Adds a path to the filter. addDataType: Adds a MIME type to the filter. An intent filter can have many actions, many categories, and so on. Here's a brief list of requirements for a match between an intent and an intent filter. This list isn't complete so, if you want a complete list, you better buy Android Application Development All-in-One For Dummies by Barry Burd. If an intent has an action, in order to match the intent, an intent filter must have an identical action. The intent filter can have additional actions. Any of these additional actions have no effect on the match.
dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht 8/9

07/07/12

Android Application Development All-in-One For Dummies Cheat Sheet - For Dummies

If an intent has categories, in order to match the intent, an intent filter must have these (and possibly more) categories. If an intent has a MIME type, in order to match the intent, an intent filter must have a matching MIME type. The intent filter can have additional MIME types. Any of these additional MIME types have no effect on the match. If an intent filter has MIME types, in order to match the intent filter, an intent must have a MIME type and the intent's MIME type must match one of the filter's MIME types. To a limited extent, the matching of MIME types can involve wildcards and regular expressions. If an intent has a URI scheme, in order to match the intent, an intent filter must have a matching URI scheme. If an intent filter has URI schemes, in order to match the intent filter, an intent must have a URI scheme and the intent's URI scheme must match one of the filter's URI schemes. To finish this list, copy the last two rules, changing a word or two in each of the copies: If an intent has a URI host, in order to match the intent, an intent filter must have a matching URI host. If an intent filter has URI hosts, in order to match the intent filter, an intent must have a URI host and the intent's URI host must match one of the filter's URI hosts. If an intent has a URI port, in order to match the intent, an intent filter must have a matching URI port. If an intent filter has URI ports, in order to match the intent filter, an intent must have a URI port and the intent's URI port must match one of the filter's URI ports. If an intent has a URI path, in order to match the intent, an intent filter must have a matching URI path. If an intent filter has URI paths, in order to match the intent filter, an intent must have a URI path and the intent's URI path must match one of the filter's URI paths.

Copyright 2012 & Trademark by John Wiley & Sons, Inc. All rights reserved.

dummies.com/how-to/content/android-application-development-for-dummies-allino.navId-407020.ht

9/9

Você também pode gostar