Your SlideShare is downloading. ×
0
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Intents in Android 5.x
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Intents in Android 5.x

157

Published on

An introduction to Intents in Android. First, the presentation introduces the concept of Intents as messages between application components. Then, the difference between implicit and explicit intents …

An introduction to Intents in Android. First, the presentation introduces the concept of Intents as messages between application components. Then, the difference between implicit and explicit intents is clearly stated, along with a description of the Intent resolution mechanism. The presentation concludes with a step-by-step tutorial on how to cast and intercept Intents from Activities.

The source code is available at https://github.com/iliocatallo/takenotes

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
157
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Intents Ilio Catallo, Eleonora Ciceri – Politecnico di Milano ilio.catallo@polimi.it, eleonora.ciceri@polimi.it
  • 2. Activities are tasks ¤ Each Activity should be associated with a specific task 2 view activity I can take pictures from the camera
  • 3. Applications are collection of activities ¤ Applications are made of different activities, each of which with its own responsibility 3 view activity view activity I can take pictures from the camera I allow users to compose new notes
  • 4. Activities should cooperate ¤ From time to time, an activity may be required to perform a task out of its set of responsibilities 4 view activity view activity I can’t take a picture by myself
  • 5. Activities should cooperate ¤ Wouldn’t it be great if Activities could cooperate? 5 view activity view activity I’ll back you up Let me ask for help help request
  • 6. Activities should cooperate ¤ Wouldn’t it be great if Activities could cooperate? 6 view activity view activity Here’s your picture Thank you! Picture
  • 7. Intents 7
  • 8. Intents ¤ Intents are messages that are passed between components 8 intent view activity activity view
  • 9. Intents ¤ A component may ask another component to execute some action on its behalf 9 intent view activity activity view
  • 10. Intents ¤ The message contains a description of the action to be performed 10 intent view activity activity view
  • 11. Intents ¤ As such, an intent always involves two subjects 11 intent view activity activity view
  • 12. Intents ¤ The sender, which wants an action to be performed and thus publishes a message 12 intent activity view view activity
  • 13. Intents ¤ The receiver, which is any component capable of performing the requested action 13 intent view activity view activity
  • 14. What Intents are good at ¤ Actions can be of different type: ¤ Taking pictures from the camera ¤ Starting a new web search ¤ Sending e-mails ¤ Sending SMS’s ¤ Each of these actions requires the execution of the associated Activity 14
  • 15. ExampleofAction 15 Intent Action: play video Browser application YouTube application
  • 16. Types of Intents 16
  • 17. Two ways of launching Activities... ¤ The calling Activity has only two ways of launching a second Activity 17 view activity 1 view activity 2
  • 18. ...by name ¤ By name: The calling Activity knows the exact name of the Activity to launch 18 view activity 1 view activity 2 Start activity 2
  • 19. ...or by action ¤ By action: The calling Activity wants something to be done (e.g., taking a picture) 19 view activity 1 view activity 2 I need to take a picture
  • 20. Implicit and Explicit intents ¤ Consequently, Android provides two kinds of Intents: ¤ Explicit intents, which specify the exact Activity to which the Intent should be given ¤ Implicit Intents, which do not declare the name of the Activity to start, but instead declare an action to perform 20 Start activity 2 Explicit intent Take a picture Implicit intent
  • 21. Intent resolution mechanism ¤ For implicit intents, the system determines which Activity is best to run based on the Intent’s fields ¤ If more than one Activity can handle the Intent, a dialog for the user to select which app to use is displayed 21
  • 22. Intent resolution mechanism 22 Activity 1 Activity 2 Intent Resolution Explicit Intent: “Execute Activity3” Implicit Intent: “Send a new e-mail” Activity 3 Fancy e-mail client Humble e-mail client User decides which client to use
  • 23. Launching new Activities 23
  • 24. Launching a new Activity ¤ An Activity asks for the intervention of a second Activity by invoking on itself the startActivity() method 24 view activity view activity startActivity()
  • 25. Launching a new Activity ¤ An Activity asks for the intervention of a second Activity by invoking on itself the startActivity() method 25 view activity view activity startActivity() intent
  • 26. Launching a new Activity ¤ Activity class: 26 public class Activity { public Activity() {...} public void startActivity(Intent intent) {...} ... } startActivity() launches a new Activity based on the provided Intent object
  • 27. Launching a new Activity ¤ First, the calling Activity forges an Intent object that contains the description of the needed action ¤ The Intent could be either implicit or explicit 27 Start activity 2 Explicit intent Take a picture Implicit intent
  • 28. Launching a new Activity ¤ Once ready, the Intent object is passed to startActivity() 28 public void startActivity(Intent intent) {...} Start activity 2 Explicit intent Take a picture Implicit intent
  • 29. Launching a new Activity ¤ Once the startActivity() method is invoked ¤ Implicit intents: the resolution mechanism identifies the Activity that best matches the intent fields ¤ Explicit intents: the system receives the call and starts an instance of the Activity specifiedby the Intent 29
  • 30. 30 How to create Intents?
  • 31. Intent class ¤ Intent class: 31 public class Intent { public Intent(Context context, Class<?> cls) {...} public Intent(String action, Uri uri) {...} ... } Explicit Intent constructor Implicit Intent constructor
  • 32. Creating explicit Intents ¤ Explicit Intents are typically used for launching new Activities within the same application ¤ Nothing in the Intent object other than the name of the Activity to be launched matters ¤ All other fields (action, data, category) are null 32 Intent intent = new Intent(this, TargetActivity.class); Context The calling Activity is used as a Context object in order to determine the package where to look for the NewActivity class
  • 33. Creating implicit Intents ¤ Each implicit Intent contains information useful to the receiver for performing the job: ¤ Action: the job that the sender can require to perform ¤ Data: the data on which the job will run ¤ Category: the execution requirements (e.g., of being executed in a browser) 33 Intent in = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0223993669")); Action Making a phone call Data The phone number
  • 34. Adding extra parameters ¤ It is possible to pass some parameters to the started Activity by adding them as extra content of the Intent 34 Intent intent = ... intent.putExtra(EXTRA_MESSAGE, myMessage); startActivity(intent);
  • 35. Receiving an Intent 35
  • 36. Receiving an Intent ¤ Within the just-started Activity, you can inspect the received Intent by using the getIntent() method ¤ Useful to retrieve the data contained in the Intent ¤ This is usually done in the onCreate() method of the new Activity 36 Intent intent = getIntent(); String message = intent.getStringExtra(EXTRA_MESSAGE);
  • 37. Receiving implicit Intents ¤ How to inform the system that an Activity can handle a specific type of implict Intent? ¤ This can be done by advertising that a given Activity can manage a specific implicit Intent, i.e., by associating the Activity with an Intent filter 37
  • 38. Intent filter ¤ An Intent filter: ¤ declares the capability of an Activity of receiving implicit Intents of a specific type ¤ delimits the Intents an Activity can handle 38 view activity I can take pictures from the camera
  • 39. Example: dialing a number ¤ In order to declare an Intent filter, you need to add an <intent-filter> element in your manifest file for the corresponding <activity> element ¤ Example: we declare that the Activity can act as a dialer 39 <intent-filter> <action android:name="android.intent.action.DIAL" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="tel"/> </intent-filter>
  • 40. Example: dialing a number ¤ Everytime an implicit intent of type android.intent.action.DIAL is casted: ¤ Our Activity is listed as a candidate: 40 Intent in = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0223993669")); startActivity(in); Example: dialing a number (2) !  Everytime an implicit intent of type android.intent.action.DIAL is casted: !  Our Activity is listed as a candidate: 26 Intent in = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0223993669")); startActivity(in);
  • 41. Example: launching an app ¤ Look in your AndroidManifest.xml file: ¤ You will find an intent filter associated with the main Activity ¤ The intent filters declares that the main activity can receive an implicit Intent coming from the Launcher ¤ Such a implicit intent corresponds to the user’s action of launching the application 41 <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
  • 42. Returning data from an Activity 42
  • 43. Requesting a result from an Activity ¤ Starting another Activity does not have to be one-way ¤ You can start a second Activity and receive a result back ¤ Examples ¤ Start the camera and receive the photo as a result ¤ Start the People app and receive a contact details as a result 43
  • 44. Requesting a result from an Activity 44 Activity 1 Camera Activity Implicit Intent: “Take a picture” Calling (first) Activity Called (second) Activity
  • 45. Activity class ¤ Activity class: 45 public class Activity { public Activity() {...} public void startActivity(Intent intent) {...} public void startActivityForResult(Intent intent, int requestCode) {...} ... } startActivityForResult() launches a new Activity, expecting to receive a result back
  • 46. Requesting a result from an Activity ¤ The Intent you pass is the same you could use to start an Activity without requiring a result back ¤ Just call startActivityForResult() instead of startActivity() ¤ You need to pass an additional integer argument, that represents a request code that identifies the request 46 Intent intent = ... startActivityForResult(intent, MY_REQUEST_CODE);
  • 47. Returned data ¤ Once the user is done with the called Activity, the result will be sent back to the first Activity ¤ Along with the actual result, additional information is returned from the called Activity to the calling Activity: ¤ The request code originally sent by the calling Activity ¤ A result code to notify that the operation in the called Activity has been successful ¤ Insight: Activity result codes have the same role as the HTTP status codes 47
  • 48. Returned data 48 Activity 1 Camera Activity (android.media.action.IMAGE_CAPTURE, TAKE_PICTURE*) Calling (first) Activity Called (second) Activity < , TAKE_PICTURE, RESULT_OK> * where TAKE_PICTURE is a int constant representing the request code
  • 49. Activity class ¤ Activity class: 49 public class Activity { public Activity() {...} public void startActivity(Intent intent) {...} public void startActivityForResult(Intent intent, int requestCode) {...} protected void onActivityResult(int requestCode, int resultCode, Intent data) {...} ... } result is processed in onActivityResult()
  • 50. Processing the returned data ¤ When the second Activity returns, the system invokes the onActivityResult() method on the calling Activity ¤ The data are sent back to the calling Activity as an Intent 50 @Override protected void onActivityResult(int requestCode, int resultCode,Intent data) { if (requestCode == MY_REQUEST_CODE) if (resultCode == RESULT_OK) [...] }
  • 51. Role of the request code ¤ The calling Activity uses different request codes to distinguish between different types of requests ¤ When receiving a result, the calling Activity uses the request code to identify the provenance of the result ¤ The request codes only makes sense for the calling Activity, while they do not have any particular meaning for the called Activity 51
  • 52. Role of the request code 52 Activity 1 Activity 4 Activity 3 Activity 2 Request code: 1 Request code: 2 Request code: 3
  • 53. Role of the request code 53 Request code: 1 Activity 2 Activity 1 Activity 4 Activity 3 The data come from Activity 2, as the request code bundled with the result is 1
  • 54. Returning data from an Activity ¤ In case the called Activity is part of our application, we have to deal also with the problem of returning data to the calling Activity ¤ We need to: ¤ Perform the operation for which we launched the second Activity ¤ Pack the result into an Intent object ¤ Determine the result code ¤ Terminate the called Activity 54
  • 55. Performing the operation in the called Activity ¤ An Activity represents a task the user can achieve ¤ To this end, it may be possible for the calling Activity to provide some parameters to the second Activity ¤ These parameters are sent within the Intent as extra’s ¤ We already know how to inspect the triggering Intent within the second Activity: 55 Intent intent = getIntent(); String message = intent.getStringExtra(EXTRA_MESSAGE);
  • 56. Packing the result into an Intent object ¤ The called Activity produces a result of some kind ¤ These data can be packed into an Intent by using the Intent’s putExtra() method 56 Intent intent = new Intent(); intent.putExtra("KeyForResult1", result1); intent.putExtra("KeyForResult2", result2); We use the default constructor, since we do not know anything about the calling Activity (but the OS knows for us)
  • 57. Activity class ¤ Activity class: 57 public class Activity { public Activity() {...} public void startActivity(Intent intent) {...} public void startActivityForResult(Intent intent, int requestCode) {...} protected void onActivityResult(int requestCode, int resultCode, Intent data) {...} public final void setResult(int resultCode, Intent data) {...} } set the result that the Activity will return to the caller
  • 58. Determining the result code ¤ The operation performed in the called Activity may succeed or fail, and the resulting data may change accordingly ¤ You can set the result and the corresponding result code as follows: 58 Intent data = new Intent(); data.putExtra(...) .... setResult(RESULT_OK, data); We use the default constructor, since we do not know anything about the calling Activity (but the OS knows for us)
  • 59. Terminating the called Activity ¤ The Intent containing the result will not be fired until the called Activity is running ¤ It is the Operating System (OS) that passes Intents between different components ¤ No communication occurs as long as the control does not return to the OS ¤ Hence, when the user is done with the Activity, it has to be terminated 59
  • 60. Terminating the called Activity ¤ Usually, it is up to the Operating System to decide when to terminate an Activity ¤ However, is some cases the user performs an operation of the UI that indicates that he is done with the current Activity ¤ To manually terminate an Activity we can use the finish() method 60 Intent data = new Intent(); data.putExtra(...) .... setResult(RESULT_OK, data); finish()
  • 61. Activity class ¤ Activity class: 61 public class Activity { public Activity() {...} public void startActivity(Intent intent) {...} public void startActivityForResult(Intent intent, int requestCode) {...} protected void onActivityResult(int requestCode, int resultCode, Intent data) {...} public final void setResult (int resultCode, Intent data) {...} public void finish () {...} } Call this when your activity is done and should be closed.
  • 62. TakeNotes V3 62
  • 63. TakeNotes v3: require a new to do ¤ When the + button is clicked, a new to do is required ¤ The input form containing the to do data is launched in a second Activity (AddToDoActivity) ¤ We require the execution of the second Activity, expecting it to return a result (i.e., the to do description) 63 Intent intent = new Intent(this, AddToDoActivity.class); startActivityForResult(intent, REQUIRE_NEW_TODO); Request code, stored in ToDoListActivity.java: public static final int REQUIRE_NEW_TODO = 0;
  • 64. TakeNotes v3: input the new to do ¤ The interface is built in the layout file (res/layout/) 64 <LinearLayout [...]> <EditText android:id="@+id/toDoContent" [...]/> <Button [...] android:onClick="addToDoToList"/> </LinearLayout>
  • 65. TakeNotes v3: returning the result ¤ When the user clicks on the button, the to-do item has to return to the first activity (ToDoListActivity) ¤ To do so, we collect the result from the AddToDoActivity activity and send it back with an Intent ¤ The Intent will report the to-do description as extra content ¤ Once the Intent is sent, the AddToDoActivity activity is closed using the finish() instruction 65
  • 66. TakeNotes v3: returning the result 66 public void addToDoToList(View view) { Intent intent = new Intent(this, ToDoListActivity.class); EditText editText = (EditText) findViewById(R.id.toDoContent); String newContent = editText.getText().toString(); intent.putExtra(TODO_CONTENT, newContent); setResult(RESULT_OK, intent); finish(); } Called when the button is pressed Terminate the Activity and release the intent
  • 67. @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == REQUIRE_NEW_TODO && resultCode == RESULT_OK) if (intent != null) addNewToDoToList( intent.getStringExtra(AddToDoActivity.TODO_CONTENT)); } TakeNotes v3: receiving the result 67 ¤ The result is now returned to ToDoListActivity and needs to be processed ¤ All the results coming from an Intent are collected by the onActivityResult method Verify the request code so as to identify the correct result
  • 68. TakeNotes v3 adding to-do’s ¤ We retrieved the item description, not the checkbox ¤ The next steps are: ¤ Build the checkbox ¤ Add the checkbox to the list linear layout 68 CheckBox newToDoCheckBox = new CheckBox(this); newToDoCheckBox.setText(message); addOnClickListener(newToDoCheckBox); LinearLayout linearLayout = (LinearLayout) findViewById(R.id.checkBoxContainer); linearLayout.addView(newToDoCheckBox); See TakeNotes v2
  • 69. References 69
  • 70. References ¤ Android Training, Starting an Activity http://developer.android.com/training/basics/firstapp/starting- activity.html ¤ Android Training, Getting a Result from an Activity http://developer.android.com/training/basics/intents/result.html ¤ Android API Guides, Intent and Intent Filters http://developer.android.com/guide/components/intents- filters.html ¤ Android Training, Sending the User to Another App http://developer.android.com/training/basics/intents/sending.html ¤ Android Training, Allowing other Apps to Start Your Activity http://developer.android.com/training/basics/intents/filters.html 70

×