Android Lesson 3 - Intent


Published on

How to use intents in Android.

1 Comment
  • Hi, very nice and perfect description of concept ...
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Android Lesson 3 - Intent

  1. 1. Android - Lesson 3 Daniela da Cruz Universidade Lusófona do Porto October, 20121 of 10
  2. 2. Intents Explicit Intents Implicit Intents Sending data to Intents Retrieving data from Intents Using Intents to call Activities Calling Sub-Activities for result data2 of 10
  3. 3. IntentsIntents are asynchronous messages which allow Android componentsto request functionality from other components of the Android system.For example an Activity can send an Intents to the Android systemwhich starts another Activity.An Intent can also contain data. This data can be used by thereceiving component.There are two types of Intents: Explit and Implict.3 of 10
  4. 4. Explicit Intents Explicit Intents explicitly defines the component which should be called by the Android system, by using the Java class as identifier. The following shows an explicit Intent.1 I n t e n t i = new I n t e n t ( t h i s , A c t i v i t y T w o . c l a s s ) ; Explicit Intents are typically used within on application as the classes in an application are controlled by the application developer. 4 of 10
  5. 5. Implicit Intents Implicit Intents do not directly specify the Android components which should be called. For example the following tells the Android system to view a webpage.1 I n t e n t i = new I n t e n t ( I n t e n t . ACTION_VIEW , U r i . p a r s e ( " h t t p : / /www . g o o g l e . com" ) ) ; or telling the Android system to open the camera: I n t e n t i = new I n t e n t ( a n d r o i d . p r o v i d e r . M e d i a S t o r e . ACTION_IMAGE_CAPTURE) ; 5 of 10
  6. 6. Implicit IntentsIf these Intents are send to the Android system it searches for allcomponents which are registered for the specific action and the datatype.If only one component is found, Android starts this componentdirectly. If several components are identifier by the Android system,the user will get an selection dialog and can decide which componentshould be used for the Intent.6 of 10
  7. 7. Sending data to Intents To send data from one intent to another, we need to use the putExtra method. This method works like an Hashtable: it expects to receive as arguments a key and a value. The following shows how to send information to an intent:1 I n t e n t i = new I n t e n t ( t h i s , A c t i v i t y T w o . c l a s s ) ; i . putExtra ( " key1 " , 1) ;3 i . putExtra ( " key2 " , " h e l l o " ) ; i . p u t E x t r a ( " k e y 3 " , new c h a r [ ] { ’ a ’ , ’ b ’ } ) ;5 ... 7 of 10
  8. 8. Retrieving data from Intents The component which receives the Intent can use the getIntent().getExtras() method call to get the extra data.1 Bundle e x t r a s = g e t I n t e n t ( ) . g e t E x t r a s ( ) ; i f ( e x t r a s != n u l l )3 { int val1 = e x t r a s . g e t I n t ( " key1 " ) ;5 S t r i n g v a l 2 = e x t r a s . g e t S t r i n g ( " key2 " ) ; char [ ] v a l 3 = e x t r a s . getCharArray ( " key3 " ) ;7 } There are other methods that can be used to retrieve data from an intent: http: // 8 of 10
  9. 9. Using Intents to call ActivitiesIf you send an Intent to the Android system, Android requires that youtell it to which type of component your Intent should be send.To start an Activity use the method startActivity(Intent). Thismethod is defined on the Context object and available in everyActivity object.If you call an Activity with the startActivity(Intent) method thecaller requires no result from the called Activity.9 of 10
  10. 10. Calling Sub-Activities for result dataIf you need some information from the called Activity use thestartActivityForResult() method.If you use the startActivityForResult() method then the startedActivity is called a Sub-Activity.10 of 10