Professional Documents
Culture Documents
Lars Vogel
Version 2.7 Copyright 2009, 2010, 2011, 2012 Lars Vogel by Lars Vogel Tutorial
306
17.07.2012
Revision History Revision 0.1 20.07.2010 Lars Vogel
Using Intents in Android This tutorials describes what Intents are and how to use them in Android. It is based on Eclipse 3.7, Java 1.6 and Android 4.0.4 (Ice Cream Sandwich).
Table of Contents
1. Android Intents 1.1. Overview 1.2. Explicit Intents 1.3. Implicit Intents 1.4. Data Transfer 2. Using Intents to call Activities 2.1. Calling Activities 2.2. Calling Sub-Activities for result data 3. Defining Intent Filters 3.1. Overview 3.2. Restrictions as of Android 3.1 4. Intents as event triggers 5. Share Intent and ShareActionProvider 6. Finding out if an Intent is available 7. Prerequisites for this tutorial 8. Tutorial: Explicit intents and data transfer between activities 9. Tutorial: Implicit Intents 10. Tutorial: Registering an IntentFilter 11. Tutorial: Picking an Image via Intent 12. Thank you 13. Questions and Discussion 14. Links and Literature 14.1. Source Code 14.2. Android Resources 14.3. vogella Resources
1. Android Intents
1.1. Overview
I t n sare asynchronous messages which allow Android components to request functionality from other net components of the Android system. For example an A t v t can send an I t n sto the Android ciiy net system which starts another A t v t . ciiy Therefore I t n sallow to combine loosely coupled components to perform certain tasks. net
www.vogella.com/articles/AndroidIntent/article.html 1/13
I t n scan be used to signal to the Android system that a certain event has occurred. Other components net in Android can register to this event and will get notified. I t n sare instances of the a d o d c n e t I t n class. net nri.otn.net I t n sare send to the Android system. Depending on how the I t n was constructed the Android net net system will run an receiver determination and determine what to do. An I t n can also contain data. This data can be used by the receiving component. For example your net application can calls via an I t n a browser component. As data it may send the URL to the browser net
vogella.com
component. Tutorials
Training
Books
Connect
Explicit I t n sare typically used within on application as the classes in an application are controlled by net the application developer.
If these I t n sare send to the Android system it searches for all components which are registered for the net specific action and the data type. If only one component is found, Android starts this component directly. If several components are identifier by the Android system, the user will get an selection dialog and can decide which component should be used for the I t n . net
The component which receives the I t n can use the g t n e t ) g t x r s )method call to get net eItn(.eEta( the extra data.
Bnl eta =gtnet)gtxrs) ude xrs eItn(.eEta(; i (xrs= nl){ f eta = ul rtr; eun
www.vogella.com/articles/AndroidIntent/article.html
2/13
} / Gtdt vateky / e aa i h e Srn vle =eta.eSrn(netETATX) tig au1 xrsgttigItn.XR_ET; i (au1! nl){ f vle = ul / D smtigwt tedt / o oehn ih h aa }
If you use the s a t c i i y o R s l ( method then the started A t v t is called a S b trAtvtFreut) ciiy uAtvt. ciiy If the Sub-Activity is finished it can send data back to its caller via Intent. This is done in the f n s ( iih) method.
@vrie Oerd p b i v i fns( { u l c o d iih) / Peaedt itn / rpr aa net Itn dt =n w Itn(; net aa e net) dt.uEta"eune1,"wnigo asa."; aaptxr(rtrKy" Sign n tr ) dt.uEta"eune2,"o cudb bte te yuae "; aaptxr(rtrKy" Yu ol e etr hn o r. ) / Atvt fnse o,rtr tedt / ciiy iihd k eun h aa steutRSL_K dt) eRsl(EUTO, aa; s p r fns(; u e .iih) }
Once the S b A t v t finished, the o A t v t R s l ( method in the calling A t v t will be u-ciiy nciiyeut) ciiy called.
@vrie Oerd p o e t d v i oAtvtRsl( n rqetoe i t rslCd,Itn dt){ rtce o d nciiyeuti t eusCd, n eutoe net aa i (eutoe= RSL_K& rqetoe= RQETCD){ f rslCd = EUTO & eusCd = EUS_OE i (aahsxr(rtrKy"){ f dt.aEta"eune1) TatmkTx( h s dt.eEta(.eSrn(rtrKy", os.aeett i , aagtxrs)gttig"eune1) TatLNT_HR)so(; os.EGHSOT.hw) } } }
filters. It can also contain additional metadata. The following will register an A t v t for the I t n which is triggered when someone wants to open a ciiy net webpage.
<ciiyadodnm=.rweAtvty atvt nri:ae"Bosrciii" adodlbl"srn/p_ae> nri:ae=@tigapnm" <netfle> itn-itr <cinadodnm=adoditn.cinVE"/ ato nri:ae"nri.netato.IW > <aeoyadodnm=adoditn.aeoyDFUT / ctgr nri:ae"nri.netctgr.EAL" > <aaadodshm=ht"> dt nri:cee"tp/ <itn-itr /netfle> <atvt> /ciiy
The following example will register an Activity for the A T O _ E Dintent for the t x / l i mime type. CINSN etpan
<ciiy atvt adodnm=.ciiyet nri:ae"AtvtTs" adodlbl"srn/p_ae > nri:ae=@tigapnm" <netfle> itn-itr <cinadodnm=adoditn.cinSN"/ ato nri:ae"nri.netato.ED > <aeoyadodnm=adoditn.aeoyDFUT / ctgr nri:ae"nri.netctgr.EAL" > <aaadodmmTp=tx/li"/ dt nri:ieye"etpan > <itn-itr /netfle> <atvt> /ciiy
If a component does not define I t n filters, it can only be called by explicit I t n s net net.
www.vogella.com/articles/AndroidIntent/article.html
4/13
r t r tu; e u n re }
p b i v i sthrItn( { u l c o d eSaenet) Itn itn =n w Itn(netATO_ED; net net e netItn.CINSN) itn.eTp(tx/li"; netstye"etpan) itn.uEtaItn.XR_ET "esg"; netptxr(netETATX, Msae) poie.eSaenetitn) rvdrsthrItn(net; }
Unfortunately this does not seem to work in the Android emulator, see Bug report for details .
Based on the result you can adjust your application for example you could disable or hide certan menu items.
Citibank Credit Card Rs 1400 Worth Welcome Offer & Earn 10 Times The Reward Points. Apply! citi.com/CreditCard Android Mobiles Sale Starting Rs 4900. 1 YR Warranty EMI or Pay on Delivery. Buy Now! www.HomeShop18.com/Android_Mobiles
www.vogella.com/articles/AndroidIntent/article.html
5/13
<utn Bto adodlyu_oun"" nri:aotclm=1 adodlyu_rvt=rgt nri:aotgaiy"ih" adodwdh"0p nri:it=8d" adodoCik"nlc" nri:nlc=oCik adodtx=Fns"/ nri:et"iih > <GiLyu> /rdaot
<mnfs> /aiet
p b i c a s AtvtOee t n s Atvt { ulc ls ciiyn xed ciiy p i a e s a i f n l i t RQETCD =1; rvt ttc ia n EUS_OE 0
/*Cle we teatvt i frtcetd * * ald hn h ciiy s is rae. / @vrie Oerd p b i v i oCet(udesvdntnett){ u l c o d nraeBnl aeIsacSae s p r oCet(aeIsacSae; u e .nraesvdntnett) stotnVe(.aotmi) eCnetiwRlyu.an; } p b i v i oCikVe ve){ u l c o d nlc(iw iw Itn i=n w Itn( h s AtvtToc a s ; net e nett i , ciiyw. l s )
www.vogella.com/articles/AndroidIntent/article.html
6/13
iptxr(Vle" "hsvleoefrAtvtTo"; .uEta"au1, Ti au n o ciiyw ) iptxr(Vle" "hsvletoAtvtTo) .uEta"au2, Ti au w ciiyw"; / Stterqetcd t aycd yulk,yucnietf te / e h eus oe o n oe o ie o a dniy h / clbc vati cd / alak i hs oe satciiyoRsl(,RQETCD) trAtvtFreuti EUS_OE; } @vrie Oerd p o e t d v i oAtvtRsl( n rqetoe i t rslCd,Itn dt){ rtce o d nciiyeuti t eusCd, n eutoe net aa i (eutoe= RSL_K& rqetoe= RQETCD){ f rslCd = EUTO & eusCd = EUS_OE i (aahsxr(rtrKy"){ f dt.aEta"eune1) TatmkTx( h s dt.eEta(.eSrn(rtrKy os.aeett i , aagtxrs)gttig"eune1 ", ) TatLNT_HR)so(; os.EGHSOT.hw) } } } } p c a e d.oel.nri.netepii; akg evglaadoditn.xlct ipr mot ipr mot ipr mot ipr mot ipr mot adodapAtvt; nri.p.ciiy adodcnetItn; nri.otn.net adodo.ude nri.sBnl; adodve.iw nri.iwVe; adodwde.dtet nri.igtEiTx;
/*Cle we teatvt i frtcetd * * ald hn h ciiy s is rae. / @vrie Oerd p b i v i oCet(udebnl){ u l c o d nraeBnl ude s p r oCet(ude; u e .nraebnl) stotnVe(.aotscn) eCnetiwRlyu.eod; Bnl eta =gtnet)gtxrs) ude xrs eItn(.eEta(; i (xrs= nl){ f eta = ul rtr; eun } Srn vle =eta.eSrn(Vle"; tig au1 xrsgttig"au1) Srn vle =eta.eSrn(Vle"; tig au2 xrsgttig"au2) i (au1! nl & vle ! nl){ f vle = ul & au2 = ul EiTx tx1=(dtet fnVeBI(.dipt) dtet et EiTx) idiwydRi.nu1; EiTx tx2=(dtet fnVeBI(.dipt) dtet et EiTx) idiwydRi.nu2; tx1stetvle) et.eTx(au1; tx2stetvle) et.eTx(au2; } } p b i v i oCikVe ve){ u l c o d nlc(iw iw fns(; iih) } @vrie Oerd p b i v i fns( { u l c o d iih) Itn dt =n w Itn(; net aa e net) / Rtr sm hr-oe vle / eun oe adcdd aus dt.uEta"eune1,"wnigo asa."; aaptxr(rtrKy" Sign n tr ) dt.uEta"eune2,"o cudb bte te yuae "; aaptxr(rtrKy" Yu ol e etr hn o r. ) steutRSL_K dt) eRsl(EUTO, aa; s p r fns(; u e .iih) } }
Run your application. The first will send data to the second A t v t which will be shown on the user ciiy interface. If you select back on your phone, the first A t v t will display a T a twith the data from the ciiy os second A t v t . ciiy
www.vogella.com/articles/AndroidIntent/article.html
7/13
<GiLyu> /rdaot
To be able to use certain intents you need to register then for your application. Maintain the following
Adodaietxl nriMnfs.m. <xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <aietxlsadod"tp/shmsadodcmakrsadod mnfs mn:nri=ht:/cea.nri.o/p/e/nri" p c a e "evglaadoditn.mlct a k g =d.oel.nri.netipii" adodvrinoe"" nri:esoCd=1 adodvrinae"."> nri:esoNm=10 <ssskadodmndVrin"5 / ue-d nri:iSkeso=1" > <sspriso adodnm=adodpriso.ALPIIEE"> ue-emsin nri:ae"nri.emsinCL_RVLGD <ue-emsin /sspriso> <sspriso adodnm=adodpriso.ALPOE > ue-emsin nri:ae"nri.emsinCL_HN" <ue-emsin /sspriso> <sspriso adodnm=adodpriso.AEA > ue-emsin nri:ae"nri.emsinCMR" <ue-emsin /sspriso> <sspriso adodnm=adodpriso.EDCNAT"> ue-emsin nri:ae"nri.emsinRA_OTCS <ue-emsin /sspriso> <sspriso adodnm=adodpriso.NENT/ ue-emsin nri:ae"nri.emsinITRE"> <plcto apiain adodio=@rwbeio" nri:cn"daal/cn adodlbl"srn/p_ae > nri:ae=@tigapnm" <ciiy atvt adodnm=.alnetAtvt" nri:ae"ClItnsciiy adodlbl"srn/p_ae > nri:ae=@tigapnm" <netfle> itn-itr <cinadodnm=adoditn.cinMI"/ ato nri:ae"nri.netato.AN > <aeoyadodnm=adoditn.aeoyLUCE"/ ctgr nri:ae"nri.netctgr.ANHR > <itn-itr /netfle> <atvt> /ciiy <apiain /plcto> <mnfs> /aiet
Change your activity to the following. We will start the new intent with the method startActivityForResult() which allow us to specify a desired result code. Once the intent is finished the method onActivityResult() is called and you can perform actions based on the result of the activity.
p c a e d.oel.nri.netipii; akg evglaadoditn.mlct ipr mot ipr mot ipr mot ipr mot ipr mot ipr mot ipr mot ipr mot adodapAtvt; nri.p.ciiy adodcnetItn; nri.otn.net adodntUi nri.e.r; adodo.ude nri.sBnl; adodve.iw nri.iwVe; adodwde.raAatr nri.igtArydpe; adodwde.pne; nri.igtSinr adodwde.os; nri.igtTat
p b i c a s ClItnsciiye t n s Atvt { ulc ls alnetAtvt xed ciiy p i a e Sinrsinr rvt pne pne;
/*Cle we teatvt i frtcetd * * ald hn h ciiy s is rae. / @vrie Oerd p b i v i oCet(udesvdntnett){ u l c o d nraeBnl aeIsacSae s p r oCet(aeIsacSae; u e .nraesvdntnett) stotnVe(.aotmi) eCnetiwRlyu.an; sinr=(pne)fnVeBI(.dsinr; pne Sinr idiwydRi.pne) Arydpe aatr=Arydpe.raermeoret i , raAatr dpe raAatrcetFoRsuc( h s
www.vogella.com/articles/AndroidIntent/article.html
8/13
Raryitns adodRlyu.ipesinrie) .ra.net, nri..aotsml_pne_tm; aatrstrponiweoreadodRlyu.ipesinrdodw_t dpe.eDoDwVeRsuc(nri..aotsml_pne_rponie m; ) sinrstdpe(dpe) pne.eAatraatr; } p b i v i oCikVe ve){ u l c o d nlc(iw iw i t psto =sinrgteetdtmoiin) n oiin pne.eSlceIePsto(; Itn itn =nl; net net ul s i c (oiin { w t h psto) cs 0 ae : itn =n w Itn(netATO_IW net e netItn.CINVE, Uipre"tp/wwvglacm); r.as(ht:/w.oel.o") bek ra; cs 1 ae : itn =n w Itn(netATO_AL net e netItn.CINCL, Uipre"e:+91358") r.as(tl(4)2479); bek ra; cs 2 ae : itn =n w Itn(netATO_IL net e netItn.CINDA, Uipre"e:+91358") r.as(tl(4)2479); satciiyitn) trAtvt(net; bek ra; cs 3 ae : itn =n w Itn(netATO_IW net e netItn.CINVE, Uipre"e:013713?=9); r.as(go5.2,.44z1") bek ra; cs 4 ae : itn =n w Itn(netATO_IW net e netItn.CINVE, Uipre"e:,?=ur") r.as(go00qqey); bek ra; cs 5 ae : itn =n w Itn(adodmdaato.MG_ATR"; net e net"nri.ei.cinIAECPUE) bek ra; cs 6 ae : itn =n w Itn(netATO_IW net e netItn.CINVE, Uipre"otn:/otcspol/); r.as(cnet/cnat/epe") bek ra; cs 7 ae : itn =n w Itn(netATO_DT net e netItn.CINEI, Uipre"otn:/otcspol/") r.as(cnet/cnat/epe1); bek ra; } i (net! nl){ f itn = ul satciiyitn) trAtvt(net; } } @vrie Oerd p b i v i oAtvtRsl( n rqetoe i t rslCd,Itn dt){ u l c o d nciiyeuti t eusCd, n eutoe net aa i (eutoe= Atvt.EUTO & rqetoe= 0 { f rslCd = ciiyRSL_K & eusCd = ) Srn rsl =dt.oR(; tig eut aatUI) TatmkTx( h s rsl,TatLNT_OG; os.aeett i , eut os.EGHLN) } } }
If you start your application you should see an list of buttons and if you press the button, different activities should be performed. Note that you didn't specify any receiving application only the thing that should be done.
www.vogella.com/articles/AndroidIntent/article.html
9/13
<netfle> itn-itr <cinadodnm=adoditn.cinVE"/ ato nri:ae"nri.netato.IW > <aeoyadodnm=adoditn.aeoyDFUT / ctgr nri:ae"nri.netctgr.EAL" > <aaadodshm=ht"/ dt nri:cee"tp > <itn-itr /netfle> <atvt> /ciiy <apiain /plcto> <mnfs> /aiet
Install your application. If you now trigger an Intent to open an URL your should be able to select your own component. You can for example trigger this I t n via the example from the implicit tutorials. net
p c a e d.oel.nri.netbosritr akg evglaadoditn.rwefle; i p r jv.oBfeeRae; m o t aai.ufrdedr i p r jv.oIpttemedr m o t aai.nuSraRae; i p r jv.e.R; m o t aantUL ipr mot ipr mot ipr mot ipr mot ipr mot ipr mot adodapAtvt; nri.p.ciiy adodcnetItn; nri.otn.net adodntUi nri.e.r; adodo.ude nri.sBnl; adodo.titoe nri.sSrcMd; adodwde.etiw nri.igtTxVe;
p b i c a s Bosrciiye t n s Atvt { ulc ls rweAtvt xed ciiy /*Cle we teatvt i frtcetd * * ald hn h ciiy s is rae. / @vrie Oerd p b i v i oCet(udesvdntnett){ u l c o d nraeBnl aeIsacSae s p r oCet(aeIsacSae; u e .nraesvdntnett) / T ke ti eapesml,w alwntokacs / o ep hs xml ipe e lo ewr ces / i teue itraetra / n h sr nefc hed SrcMd.hedoiyplc =n w SrcMd.hedoiyBidr) titoeTraPlc oiy e titoeTraPlc.ule( .emtl(.ul(; priAl)bid) SrcMd.eTraPlc(oiy; titoesthedoiyplc) stotnVe(.aotmi) eCnetiwRlyu.an; Itn itn =gtnet) net net eItn(; TxVe tx =(etiw fnVeBI(.dtxVe) etiw et TxVe) idiwydRi.etiw; / T gtteato o teitn ue / o e h cin f h net s Srn ato =itn.eAto(; tig cin netgtcin) i (cin! Itn.CINVE){ f ato = netATO_IW t r w n w Rniexeto(Sol nthpe"; ho e utmEcpin"hud o apn) } / T gttedt ue / o e h aa s Uidt =itn.eDt(; r aa netgtaa) ULul R r; ty{ r ul=n w ULdt.eShm(,dt.eHs(,dt.ePt() r e R(aagtcee) aagtot) aagtah); BfeeRae r =n w BfeeRae( e Ipttemedrulo ufrdedr d e ufrdedrn w nuSraRae(r.p eSra(); ntem)) Srn ln ="; tig ie " w i e (ln =r.edie) ! nl){ hl (ie draLn() = ul tx.pedln) etapn(ie; } }c t h (xeto e { ac Ecpin ) epittcTae) .rnSakrc(; } } }
If you select you component the HTML code should be loaded into your text view.
www.vogella.com/articles/AndroidIntent/article.html
10/13
www.vogella.com/articles/AndroidIntent/article.html
11/13
p b i c a s Iaeikciiye t n s Atvt { ulc ls mgPcAtvt xed ciiy p i a e s a i f n l i t RQETCD =1 rvt ttc ia n EUS_OE ; p i a e Bta bta; rvt imp imp p i a e Iaeiwiaeiw rvt mgVe mgVe;
/*Cle we teatvt i frtcetd * * ald hn h ciiy s is rae. / @vrie Oerd p b i v i oCet(udesvdntnett){ u l c o d nraeBnl aeIsacSae s p r oCet(aeIsacSae; u e .nraesvdntnett) stotnVe(.aotmi) eCnetiwRlyu.an; iaeiw=(mgVe)fnVeBI(.drsl) mgVe Iaeiw idiwydRi.eut; } p b i v i pcIaeVe Ve){ u l c o d ikmg(iw iw Itn itn =n w Itn(; net net e net) itn.eTp(iae*) netstye"mg/"; itn.eAto(netATO_E_OTN) netstcinItn.CINGTCNET; itn.dCtgr(netCTGR_PNBE; netadaeoyItn.AEOYOEAL) satciiyoRsl(net RQETCD) trAtvtFreutitn, EUS_OE; } @vrie Oerd p o e t d v i oAtvtRsl( n rqetoe i t rslCd,Itn dt){ rtce o d nciiyeuti t eusCd, n eutoe net aa Ipttemsra =nl; nuSra tem ul i (eusCd = RQETCD & rslCd = Atvt.EUTO) f rqetoe = EUS_OE & eutoe = ciiyRSL_K ty{ r / W ne t rcl uue btas / e ed o eye nsd imp i (imp! nl){ f bta = ul bta.eyl(; imprcce) } sra =gtotnRsle(.pnnuSra(aagtaa) tem eCneteovr)oeIpttemdt.eDt( ) ; bta =BtaFcoydcdSra(tem; imp impatr.eoetemsra) iaeiwstmgBta(imp; mgVe.eIaeimpbta) }c t h (ieoFudxeto e { ac FlNtonEcpin ) epittcTae) .rnSakrc(; }f n l y { ial i (tem! nl) f sra = ul ty{ r sra.ls(; temcoe) }c t h (Oxeto e { ac IEcpin ) epittcTae) .rnSakrc(; } } s p r oAtvtRsl(eusCd,rslCd,dt) u e .nciiyeutrqetoe eutoe aa; } }
If you run this application you can select an Image from your Image Library and assign it to your I a e i w mgVe.
www.vogella.com/articles/AndroidIntent/article.html
13/13