• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Android Lesson 3 - Intent
 

Android Lesson 3 - Intent

on

  • 1,168 views

How to use intents in Android.

How to use intents in Android.

Statistics

Views

Total Views
1,168
Views on SlideShare
1,154
Embed Views
14

Actions

Likes
0
Downloads
36
Comments
0

3 Embeds 14

http://www.linkedin.com 12
http://www.verious.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Android Lesson 3 - Intent Android Lesson 3 - Intent Presentation Transcript

    • Android - Lesson 3 Daniela da Cruz Universidade Lusófona do Porto October, 20121 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 data2 of 10
    • 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
    • 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 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
    • 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 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
    • 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