SlideShare a Scribd company logo
1 of 10
Download to read offline
Android - Lesson 3




                             Daniela da Cruz

                        Universidade Lusófona do Porto


                               October, 2012


1 of 10
Intents
    Explicit Intents
    Implicit Intents
    Sending data to Intents
    Retrieving data from Intents
    Using Intents to call Activities
    Calling Sub-Activities for result data




2 of 10
Intents



Intents are asynchronous messages which allow Android components
to request functionality from other components of the Android system.
For example an Activity can send an Intents to the Android system
which starts another Activity.
An Intent can also contain data. This data can be used by the
receiving component.
There are two types of Intents: Explit and Implict.




3 of 10
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
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
Implicit Intents



If these Intents are send to the Android system it searches for all
components which are registered for the 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 Intent.




6 of 10
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
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:
    //developer.android.com/reference/android/os/Bundle.html.
     8 of 10
Using Intents to call Activities



If you send an Intent to the Android system, Android requires that you
tell it to which type of component your Intent should be send.
To start an Activity use the method startActivity(Intent). This
method is defined on the Context object and available in every
Activity object.
If you call an Activity with the startActivity(Intent) method the
caller requires no result from the called Activity.




9 of 10
Calling Sub-Activities for result data




If you need some information from the called Activity use the
startActivityForResult() method.
If you use the startActivityForResult() method then the started
Activity is called a Sub-Activity.




10 of 10

More Related Content

What's hot

What's hot (20)

Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
 
Fragment
Fragment Fragment
Fragment
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
 
Android intent
Android intentAndroid intent
Android intent
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Intent in android
Intent in androidIntent in android
Intent in android
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
Manipulating Android tasks and back stack
Manipulating Android tasks and back stackManipulating Android tasks and back stack
Manipulating Android tasks and back stack
 
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo SilvaAndroid | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android Components
Android ComponentsAndroid Components
Android Components
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Thread
ThreadThread
Thread
 
Basic android-ppt
Basic android-pptBasic android-ppt
Basic android-ppt
 
Scalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureScalable JavaScript Application Architecture
Scalable JavaScript Application Architecture
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
 
Android async task
Android async taskAndroid async task
Android async task
 
Android Navigation Component
Android Navigation ComponentAndroid Navigation Component
Android Navigation Component
 

Similar to Android Lesson 3 - Intent

Android App Development 07 : Intent & Share
Android App Development 07 : Intent & ShareAndroid App Development 07 : Intent & Share
Android App Development 07 : Intent & Share
Anuchit Chalothorn
 

Similar to Android Lesson 3 - Intent (20)

Android intents in android application-chapter7
Android intents in android application-chapter7Android intents in android application-chapter7
Android intents in android application-chapter7
 
Android App Development 07 : Intent & Share
Android App Development 07 : Intent & ShareAndroid App Development 07 : Intent & Share
Android App Development 07 : Intent & Share
 
Using intents in android
Using intents in androidUsing intents in android
Using intents in android
 
ANDROID
ANDROIDANDROID
ANDROID
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
Intents are Awesome
Intents are AwesomeIntents are Awesome
Intents are Awesome
 
Intent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).pptIntent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).ppt
 
Level 1 & 2
Level 1 & 2Level 1 & 2
Level 1 & 2
 
Ppt 2 android_basics
Ppt 2 android_basicsPpt 2 android_basics
Ppt 2 android_basics
 
Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
 
Tut123456.pdf
Tut123456.pdfTut123456.pdf
Tut123456.pdf
 
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxxMAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
 
Android Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsAndroid Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intents
 
unit3.pptx
unit3.pptxunit3.pptx
unit3.pptx
 
Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database
 
Activity & Shared Preference
Activity & Shared PreferenceActivity & Shared Preference
Activity & Shared Preference
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Android development session 2 - intent and activity
Android development   session 2 - intent and activityAndroid development   session 2 - intent and activity
Android development session 2 - intent and activity
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
 

More from Daniela Da Cruz

More from Daniela Da Cruz (9)

Introduction to iOS and Objective-C
Introduction to iOS and Objective-CIntroduction to iOS and Objective-C
Introduction to iOS and Objective-C
 
Games Concepts
Games ConceptsGames Concepts
Games Concepts
 
C basics
C basicsC basics
C basics
 
Game Development with AndEngine
Game Development with AndEngineGame Development with AndEngine
Game Development with AndEngine
 
Interactive Verification of Safety-Critical Systems
Interactive Verification of Safety-Critical SystemsInteractive Verification of Safety-Critical Systems
Interactive Verification of Safety-Critical Systems
 
Android Introduction
Android IntroductionAndroid Introduction
Android Introduction
 
Comment Analysis approach for Program Comprehension (Software Engineering Wor...
Comment Analysis approach for Program Comprehension (Software Engineering Wor...Comment Analysis approach for Program Comprehension (Software Engineering Wor...
Comment Analysis approach for Program Comprehension (Software Engineering Wor...
 
Android Lesson 2
Android Lesson 2Android Lesson 2
Android Lesson 2
 
Android Introduction - Lesson 1
Android Introduction - Lesson 1Android Introduction - Lesson 1
Android Introduction - Lesson 1
 

Android Lesson 3 - Intent

  • 1. Android - Lesson 3 Daniela da Cruz Universidade Lusófona do Porto October, 2012 1 of 10
  • 2. Intents Explicit Intents Implicit Intents Sending data to Intents Retrieving data from Intents Using Intents to call Activities Calling Sub-Activities for result data 2 of 10
  • 3. Intents Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity. An Intent can also contain data. This data can be used by the receiving component. There are two types of Intents: Explit and Implict. 3 of 10
  • 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. 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. Implicit Intents If these Intents are send to the Android system it searches for all components which are registered for the 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 Intent. 6 of 10
  • 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. 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: //developer.android.com/reference/android/os/Bundle.html. 8 of 10
  • 9. Using Intents to call Activities If you send an Intent to the Android system, Android requires that you tell it to which type of component your Intent should be send. To start an Activity use the method startActivity(Intent). This method is defined on the Context object and available in every Activity object. If you call an Activity with the startActivity(Intent) method the caller requires no result from the called Activity. 9 of 10
  • 10. Calling Sub-Activities for result data If you need some information from the called Activity use the startActivityForResult() method. If you use the startActivityForResult() method then the started Activity is called a Sub-Activity. 10 of 10