You are on page 1of 13

Android Intents - Tutorial

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

Created Revision 0.2 - 2.7 19.07.2010 - 17.07.2012 Lars Vogel

bug fixes and enhancements

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

Android supports explicit and implicit I t n s net.

1.2. Explicit Intents


Explicit I t n sexplicitly names the component which should be called by the Android system, by using the net Java class as identifier. The following shows an explicit I t n . If that I t n is correctly send to the Android system, it will start net net the associated class.
Itn i=n w Itn( h s AtvtToc a s ; net e nett i , ciiyw. l s ) iptxr(Vle" "hsvleoefrAtvtTo"; .uEta"au1, Ti au n o ciiyw ) iptxr(Vle" "hsvletoAtvtTo) .uEta"au2, Ti au w ciiyw";

Explicit I t n sare typically used within on application as the classes in an application are controlled by net the application developer.

1.3. Implicit Intents


Implicit I t n sdo not directly specify the Android components which should be called. They specify the net action which should be performed and optionally an URI which should be used for this action. For example the following tells the Android system to view a webpage. Typically the web browser is registered to this I t n but other component could also register themself to this event. net
Itn itn =n w Itn(netATO_IW Uipre"tp/wwvglacm); net net e netItn.CINVE, r.as(ht:/w.oel.o")

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

1.4. Data Transfer


An implicit I t n contains the Action and optional the URI. The receiving component can get this net information via the g t c i n )and g t a a )methods. eAto( eDt( Explicit and implicit I t n scan also contain additional data. This data call be filled by the component net which creates the I t n . It can and can get extracted by the component which receives the I t n . net net The component which creates the I t n can add data to it via the overloaded p t x r ( method. net uEta) Extras are key/value pairs; the key is always a String. As value you can use the primitive data types (int, float,..), String, Bundle, Parceable and Serializable. For example you can trigger all components which have been registered to send some data via the n w e I t n ( n e t A T O _ E D This I t n determines possible receivers via the type. What is send netItn.CINSN) net it defined via the p t x r method. You can use any String as key, the following uses the keys which are uEta predefined for the ACTION_SEND intent.
Itn itn =n w Itn(netATO_ED; net net e netItn.CINSN) itn.eTp(tx/li"; netstye"etpan) itn.uEtaadodcnetItn.XR_ET "esfryu"; netptxr(nri.otn.netETATX, Nw o o!) satciiyitn) trAtvt(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 }

2. Using Intents to call Activities


2.1. Calling Activities
If you send an I t n to the Android system, Android requires that you tell it to which type of component net your I t n should be send. net To start an A t v t use the method s a t c i i y I t n ) This method is defined on the ciiy trAtvt(net. C n e tobject and available in every A t v t object. otx ciiy If you call an A t v t with the s a t c i i y I t n )method the caller requires no result from the ciiy trAtvt(net called A t v t . ciiy

2.2. Calling Sub-Activities for result data


If you need some information from the called A t v t use the s a t c i i y o R s l ( method. ciiy trAtvtFreut)
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 ) 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; }

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) } } }

3. Defining Intent Filters


3.1. Overview
If an I t n is send to the Android system, it will determine suitable applications for this I t n . If several net net components have been registered for this type of I t n , Android offers the user the choice to open one of net them. This determination is based on I t n F l e . An I t n F l e specifies the types of I t n that netits netitr net an activity, service, or broadcast receiver can respond to. An I t n F l e declares the capabilities of a netitr component. It specifies what an Activity or Service can do and what types of broadcasts a Receiver can handle. It allows the corresponding component to receive Intents of the declared type. I t n F l e sare typically defined via the A d o d a i e t x lfile. For B o d a t e e v rit is netitr nriMnfs.m racsRcie also possible to define them in coding. An I t n F l e sis defined by its category, action and data netitr
www.vogella.com/articles/AndroidIntent/article.html 3/13

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.

3.2. Restrictions as of Android 3.1


As of Android 3.1 the Android system will by default exclude all B o d a t e e v rfrom receiving racsRcie I t n sif the corresponding application has never been started by the user or if the user explicitly stopped net the application via the Android menu (in Manage Application). This is an additional security features as the user can be sure that only the applications he started will receive broadcast I t n s net.

4. Intents as event triggers


I t n scan also be used to send broadcast messages into the Android system. net B o d a t e e v r can register to event and will get notified if such an event is triggered. racsRcies Your application can register to system events, e.g. a new email has arrived, system boot is complete or a phone call is received and react accordingly. As said earlier, since Android version 3.1 the Android system will per default exclude all B o d a t e e v rfrom receiving I t n sif the corresponding application has never been started by racsRcie net the user or if the user explicitly stopped the application via the Android menu (in Manage Application).

5. Share Intent and ShareActionProvider


As of Android 4.0 you can also add an Action Provider to your ActionBar which allows to share. For this you have to define a special menu entry and assign an I t n which contain the sharing data to it in your net Atvt. ciiy
<xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <euxlsadod"tp/shmsadodcmakrsadod > mn mn:nri=ht:/cea.nri.o/p/e/nri" <tmadodi=@i/eusae ie nri:d"+dmn_hr" adodtte"hr" nri:il=Sae adodsoAAto=iRo" nri:hwscin"fom adodatoPoieCas"nri.igtSaecinrvdr / nri:cinrvdrls=adodwde.hrAtoPoie" > <tm ie adodi=@i/tm" nri:d"+die1 adodsoAAto=iRo" nri:hwscin"fom adodtte"oeetis." nri:il=Mr nre..> <ie> /tm <mn> /eu @vrie Oerd p b i b o e n oCetOtoseuMn mn){ ulc ola nraepinMn(eu eu gteunltr)ifaeRmn.yeu mn) eMnIfae(.nlt(.eummn, eu; / poie i afedi yu Atvt / rvdr s il n or ciiy poie =(hrAtoPoie)mn.idtmRi.eusae rvdr Saecinrvdr eufnIe(.dmn_hr) .eAtoPoie(; gtcinrvdr) sthrItn(; eSaenet)

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 .

6. Finding out if an Intent is available


Sometimes you want to find if an application has registered for a certain I t n . For example you want to net check if a certain receiver is available and if you enable some functionality in your app. This can be done via the P c a e a a e class. The following code checks if an someone has registered akgMngr himself for a certain I t n . Construct your I t n as you desired to trigger it and pass it to the following net net method.
p b i s a i b o e n iItnAalbeCnetcx Itn itn){ ulc ttc ola snetvial(otx t, net net f n l Pcaeaae mr=cxgtakgMngr) ia akgMngr g t.ePcaeaae(; Ls<eovIf>ls = itRsleno it mrqeynetciiisitn, g.urItnAtvte(net Pcaeaae.AC_EAL_NY; akgMngrMTHDFUTOL) r t r ls.ie)>0 e u n itsz( ; }

Based on the result you can adjust your application for example you could disable or hide certan menu items.

7. Prerequisites for this tutorial


The following assumes that you have already basic knowledge in Android development. Please check the Android development tutorial to learn the basics.
Android Job Opening Apply for Companies Recruiting Now! Get Interview Calls & Job Alerts.
www.careerbuilder.co.in/Android_Job

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

8. Tutorial: Explicit intents and data transfer between activities


The following tutorial demonstrates how to use explicit I t n sand how to transfer data between two net Activities. The first A t v t will call the second one via an explicit intent. Once the user select the "Back" button on ciiy his phone the first A t v t will receive some hard-coded data from the S b A t v t . ciiy u-ciiy Create a new Android application "de.vogella.android.intent.explicit" with the A t v t called ActivityOne. ciiy Change the layout m i . m to the following. anxl
<xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <ieraotxlsadod"tp/shmsadodcmakrsadod LnaLyu mn:nri=ht:/cea.nri.o/p/e/nri" adodlyu_it=mthprn" nri:aotwdh"ac_aet adodlyu_egt"ac_aet nri:aothih=mthprn" adodoinain"etcl > nri:retto=vria" <etiw TxVe adodi=@i/etiw1 nri:d"+dTxVe0" adodlyu_it=wa_otn" nri:aotwdh"rpcnet adodlyu_egt"rpcnet nri:aothih=wa_otn" adodmnegt"0i" nri:iHih=6dp adodtx=FrtAtvt.Pesbto t cl scn atvt" nri:et"is ciiy rs utn o al eod ciiy adodtxSz=2s"> nri:etie"0p <TxVe> /etiw <utn Bto adodi=@i/utn1 nri:d"+dBto0" adodlyu_it=wa_otn" nri:aotwdh"rpcnet adodlyu_egt"rpcnet nri:aothih=wa_otn" adodoCik"nlc" nri:nlc=oCik adodtx=Cliga itn"> nri:et"aln n net <Bto> /utn <LnaLyu> /ieraot

www.vogella.com/articles/AndroidIntent/article.html

5/13

Create the layout "second.xml".


<xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <rdaotxlsadod"tp/shmsadodcmakrsadod GiLyu mn:nri=ht:/cea.nri.o/p/e/nri" adodlyu_it=mthprn" nri:aotwdh"ac_aet adodlyu_egt"ac_aet nri:aothih=mthprn" adodainetoe"lgBud" nri:lgmnMd=ainons adodclmCut"" nri:ounon=2 adodclmOdrrsre=fle nri:ounrePeevd"as" adodueealMris"re > nri:sDfutagn=tu" <etiw TxVe adodlyu_rvt=cne_oiotl nri:aotgaiy"etrhrzna" adodtx=Ipt1 nri:et"nu " adodtxSz=3dp / nri:etie"2i" > <dtet EiTx adodi=@i/nu1 nri:d"+dipt" adodlyu_rvt=fl_oiotl nri:aotgaiy"ilhrzna" adodtx=Dfut / nri:et"eal" > <etiw TxVe adodlyu_rvt=cne_oiotl nri:aotgaiy"etrhrzna" adodtx=Ipt2 nri:et"nu " adodtxSz=3dp / nri:etie"2i" > <dtet EiTx adodi=@i/nu2 nri:d"+dipt" adodlyu_rvt=fl_oiotl nri:aotgaiy"ilhrzna" adodtx=Dfut / nri:et"eal" >

<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

Declare a new A t v t called ActivityTwo via the A d o d a i e t x lfile. ciiy 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.xlct a k g =d.oel.nri.netepii" adodvrinoe"" nri:esoCd=1 adodvrinae"."> nri:esoNm=10 <ssskadodmndVrin"5 / ue-d nri:iSkeso=1" > <plcto apiain adodio=@rwbeio" nri:cn"daal/cn adodlbl"srn/p_ae > nri:ae=@tigapnm" <ciiy atvt adodnm=.ciiyn" nri:ae"AtvtOe 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 <ciiy atvt adodnm=AtvtTo nri:ae"ciiyw" adodlbl"ciiyw"> nri:ae=AtvtTo <atvt> /ciiy <apiain /plcto>

<mnfs> /aiet

Create the following coding for your two Activities.


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.os; nri.igtTat

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;

p b i c a s AtvtToe t n s Atvt { ulc ls ciiyw xed ciiy

/*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

9. Tutorial: Implicit Intents


The following tutorial demonstrates the usage of implicit i t n sof the Android System. net Create a new Android application "de.vogella.android.intent.implicit" with a A t v t called " ciiy C l I t n s c i i y". alnetAtvt In this example we will use a S i n rto select the I t n which should get triggered. For the content of pne net the S i n rwe will define static values. pne Create the following "intents.xml" file in the "res/values" folder.
<eore> rsucs <tigarynm=itns> srn-ra ae"net" <tmOe Bosr/tm ie>pn rwe<ie> <tmCl Smoe/tm ie>al oen<ie> <tmDa<ie> ie>il/tm <tmSo Mp/tm ie>hw a<ie> <tmSac o Mp/tm ie>erh n a<ie> <tmTk pcue/tm ie>ae itr<ie> <tmSo cnat<ie> ie>hw otcs/tm

www.vogella.com/articles/AndroidIntent/article.html

7/13

<tmEi frtcnat/tm ie>dt is otc<ie> <srn-ra> /tigary <rsucs /eore>

Change the m i . m layout file to the following. anxl


<xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <rdaotxlsadod"tp/shmsadodcmakrsadod GiLyu mn:nri=ht:/cea.nri.o/p/e/nri" adodlyu_it=mthprn" nri:aotwdh"ac_aet adodlyu_egt"ac_aet nri:aothih=mthprn" adodainetoe"lgBud" nri:lgmnMd=ainons adodclmCut""> nri:ounon=1 <pne Sinr adodi=@i/pne" nri:d"+dsinr adodlyu_rvt=fl_oiotl nri:aotgaiy"ilhrzna" adoddaSlcoOTp"re nri:rweetrno=tu" > <Sinr /pne> <utn Bto adodi=@i/rge" nri:d"+dtigr adodoCik"nlc" nri:nlc=oCik adodtx=TigrItn" nri:et"rge net> <Bto> /utn

<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.

10. Tutorial: Registering an IntentFilter


The following example will register a A t v t for the I t n which is triggered to view a http webpage. ciiy net Our application downloads the HTML source of this page and display this in a T x V e . etiw Create the Android project "de.vogella.android.intent.browserfilter" with the Activity called Bosrciiy rweAtvt. Register your A t v t to the I t n via the I t n . c i n V E action and the scheme "http" via ciiy net netAto_IW the following A d o d a i e t x l The manifest also declares the permission to access the Internet. 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.rwefle" a k g =d.oel.nri.netbosritr adodvrinoe"" nri:esoCd=1 adodvrinae"."> nri:esoNm=10 <ssskadodmndVrin"5 / ue-d nri:iSkeso=1" > <sspriso adodnm=adodpriso.NENT > ue-emsin nri:ae"nri.emsinITRE" <ue-emsin /sspriso> <plcto apiain adodio=@rwbeio" nri:cn"daal/cn adodlbl"srn/p_ae > nri:ae=@tigapnm" <ciiy atvt adodnm=.rweAtvt" nri:ae"Bosrciiy adodlbl"srn/p_ae > nri:ae=@tigapnm"

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

Change the m i . m layout file to the following. anxl


<xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <ieraotxlsadod"tp/shmsadodcmakrsadod LnaLyu mn:nri=ht:/cea.nri.o/p/e/nri" adodoinain"etcl nri:retto=vria" adodlyu_it=mthprn" nri:aotwdh"ac_aet adodlyu_egt"ac_aet nri:aothih=mthprn" > <etiw TxVe adodlyu_it=mthprn" nri:aotwdh"ac_aet adodlyu_egt"rpcnet nri:aothih=wa_otn" adodi=@i/etiw/ nri:d"+dtxVe"> <LnaLyu> /ieraot

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

11. Tutorial: Picking an Image via Intent


The following example will show how to pick an Image from the phone app on Android via an Intent. Create the Android project "de.vogella.android.imagepick" with the A t v t called ImagePickActivity. ciiy Change the m i . m layout file to the following. anxl
<xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <ieraotxlsadod"tp/shmsadodcmakrsadod LnaLyu mn:nri=ht:/cea.nri.o/p/e/nri" adodlyu_it=mthprn" nri:aotwdh"ac_aet adodlyu_egt"ac_aet nri:aothih=mthprn" adodoinain"etcl > nri:retto=vria" <utn Bto adodi=@i/utn" nri:d"+dbto1 adodlyu_it=wa_otn" nri:aotwdh"rpcnet adodlyu_egt"rpcnet nri:aothih=wa_otn" adodoCik"ikmg" nri:nlc=pcIae adodtx=Bto"> nri:et"utn <Bto> /utn <mgVe Iaeiw adodi=@i/eut nri:d"+drsl" adodlyu_it=mthprn" nri:aotwdh"ac_aet adodlyu_egt"ac_aet nri:aothih=mthprn" adodsc"daal/cn > nri:r=@rwbeio" <Iaeiw /mgVe> <LnaLyu> /ieraot

www.vogella.com/articles/AndroidIntent/article.html

11/13

Change your A t v t according to the following coding. ciiy


p c a e d.oel.nri.mgpc; akg evglaadodiaeik i p r jv.oFlNtonEcpin m o t aai.ieoFudxeto; i p r jv.oIEcpin m o t aai.Oxeto; i p r jv.oIpttem m o t aai.nuSra; ipr mot ipr mot ipr mot ipr mot ipr mot ipr mot ipr mot adodapAtvt; nri.p.ciiy adodcnetItn; nri.otn.net adodgahc.imp nri.rpisBta; adodgahc.impatr; nri.rpisBtaFcoy adodo.ude nri.sBnl; adodve.iw nri.iwVe; adodwde.mgVe; nri.igtIaeiw

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.

12. Thank you


Please help me to support this article:

13. Questions and Discussion


Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good
www.vogella.com/articles/AndroidIntent/article.html 12/13

questions which might also help you.

14. Links and Literature


14.1. Source Code
Source Code of Examples

14.2. Android Resources


Android Location API and Google Maps Android and Networking Android Homepage Android Issues / Bugs Android Google Groups

14.3. vogella Resources


vogella Training Android and Eclipse Training from the vogella team Android Tutorial Introduction to Android Programming GWT Tutorial Program in Java and compile to JavaScript and HTML Eclipse RCP Tutorial Create native applications in Java JUnit Tutorial Test your application Git Tutorial Put everything you have under distributed version control system

www.vogella.com/articles/AndroidIntent/article.html

13/13

You might also like