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