1 / 40

Programming with Android: Activities and Intents

Programming with Android: Activities and Intents. Luca Bedogni Marco Di Felice Dipartimento di Informatica – Scienza e Ingegneria Università di Bologna. Outline. What is an intent ?. Intent description. Handling Explicit Intents.

crossen
Download Presentation

Programming with Android: Activities and Intents

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Android: Activities and Intents Luca Bedogni Marco Di Felice Dipartimento di Informatica – Scienza e Ingegneria Università di Bologna

  2. Outline What is an intent? Intent description Handling Explicit Intents Handling implicit Intents Intent-Resolution process Intent with results: Sender side Intent with results: Receiver side 2

  3. Android Applications Design Developing an Android Application means using in a proper way the Android basic components … Activity Fragment Layout Views Content Providers Intent Broadcast Receiver Service 3

  4. Android Applications Design Developing an Android Application means using in a proper way the Android basic components … Activity Intent Intent Service Broadcast Receiver 4

  5. 5 More on Activities: Activity states In most cases, an Android Application is composed of multiple Activities, not just one … LOGIN Welcome Marco! marco Activity 1 PASSWORD Activity 2 ********** Go to Next Activity Login

  6. 6 Each Activity hasitsown: Javaimplementation XML Layout file Lifecycle with differentstates XML Tag in AndroidManifest.xml More on Activities: Savingresources MyActivity.java activity_main.xml ACTIVE PAUSED STOPPED <application> <activity android:name=".MyActivity" /> </application>

  7. 7 Activities and AndroidManifest.xml • EachactivityhasitsJavaclass and layout file. public class FirstActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); } public class SecondActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); }

  8. 8 IntentDefinition Call a component from another component Possible to pass data between components Components: Activities, Services, Broadcast receivers … Something like: “Android, please do that with these data” Reuse already installed applications and components Intent: facility for late run-time binding between components of the same or different applications.

  9. 9 IntentDefinition We can think to an Intentobjectas a messagecontaining a bundle of information. Component Name Action Name Data Structure of an Intent Category Extra Flags • Intentissent from current Activity to a receiver Activity whichisthenactivated and executed.

  10. 10 Intenttypes INTENT TYPES IMPLICIT EXPLICIT The target receiver is specified through the actions to be executed. The system chooses the receiver that matches the request. The target receiver is specified through the Component Name Used to launch specific Activities

  11. 11 Intenttypes: ExplicitIntents Explicit Intent  Specify the name of the Activity that will handle the intent. USED Component Name Action Name NOT USED Data NOT USED Structure of an Intent Category NOT USED Extra Flags • Used to control the execution flow betweenActivitiesbelonging to the sameAndroidapplication.

  12. 12 Intenttypes: ExplicitIntents • Build a new Intent message 2. Specify the Activity who will receive the Intent 3. Fire the Intent through the startActivity() NAME OF THE ACTIVITY TO START Intent intent=new Intent(this, SecondActivity.class); startActivity(intent);

  13. 13 Intenttypes: ExplicitIntents • Build a new Intentmessage 2. Specify the Activity whowillreceive the Intent 3. Fire the Intentthrough the startActivity() Intent intent=new Intent(); ComponentName component=new ComponentName(this,SecondActivity.class); intent.setComponent(component); startActivity(intent); ALTERNATIVE code

  14. 14 Intenttypes: ExplicitIntents (OPTIONAL) Insert parameters to be sent to the called Activity in the Extra field of the Intent. intent.putExtra(“KEY”, VALUE); Set an argument named “MyValue” and equal to 5. Intent intent=new Intent(this, SecondActivity.class); intent.putExtra(“myValue”,5); startActivity(intent);

  15. 15 Intenttypes: ExplicitIntents (OPTIONAL) From the called Activity, retrieve the parameters inserted from the calling Activity intent.getExtras().getTYPE(“KEY”); Get an argument of type int with key equal to “myValue” intent.getExtras().getInt(“myValue”); intent.getExtras().getString(“myString”); intent.getExtras().getBoolean(“myBoolean”);

  16. 16 Intenttypes: ExplicitIntents Some Activities can return results to the caller Activity SENDER SIDE • IMPLEMENT method onActivityResult(intrequestCode, intresultCode, Intent data) • INVOKE method startActivityForResult(Intent intent, intrequestCode) Intent intent = new Intent(this, SecondActivity.class); startActivityForResult(intent, CHOOSE_ACTIVITY_CODE); … public void onActivityResult(int requestCode, int resultCode, Intent data) { // Invoked when SecondActivity completes its operations

  17. 17 Intenttypes: ExplicitIntents Some Activities can return results to the caller Activity RECEIVER SIDE • IMPLEMENT method setResult(int resultCode, Intent data) • SET results on the Extra field of the Intent Intent intent=new Intent(); setResult(RESULT_OK, intent); intent.putExtra(”result", resultValue); finish(); The result is delivered only after invoking the finish() method!

  18. 18 Intenttypes INTENT TYPES EXPLICIT IMPLICIT The target receiver is specified through the actions to be executed. The system chooses the receiver that matches the request. The target receiver is specified through the component Name Used to launch specific Activities

  19. 19 When an Intent is launched, Android checks out which Activies might answer to the Intent … If at least one is found, then that Activity is started Binding does not occur at compile time, nor at install time, but at run-time …(late run-time binding) Intenttypes: ImplicitIntents Implicit Intents  do not name a target (component name is left blank) but an intention of what to do …

  20. 20 Intenttypes: ExplicitIntents Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … Component Name NOT USED Action Name USED Data USED Structure of an Intent Category USED Extra Flags • Used to control the execution flow betweenActivitiesbelonging to DIFFERENT Androidapplications.

  21. 21 Intenttypes: ExplicitIntents Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … Component Name NOT USED • A string naming the action to be performed. • Pre-defined, or can be specified by the programmer. • void setAction(String) Action Name USED Data USED Category USED Extra Flags

  22. 22 Intenttypes: ExplicitIntents Specialactions(http://developer.android.com/reference/android/content/Intent.html)

  23. 23 Intenttypes: ExplicitIntents Generic actions (http://developer.android.com/reference/android/content/Intent.html) • Action Defined by the programmer • it.example.projectpackage.FILL_DATA(package prefix + nameaction)

  24. 24 Intenttypes: ExplicitIntents • Build a new Intent message 2. Specify only the Action you want to perform 3. Fire the Intent through the startActivity() ACTION NAME Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivity(intent);

  25. 25 Intenttypes: ExplicitIntents • Build a new Intentmessage • Specifyonly the Actionyouwant to perform • Fire the Intentthrough the startActivity() • Verifywhether the Intent can be handled Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); }

  26. 26 Intenttypes: ExplicitIntents Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … Component Name NOT USED • Data passed from the caller to the called Component. • Def. of the data (URI) and Type of the data (MIME type) • void setData(Uri) • void setType(String) Action Name USED Data USED Category USED Extra Flags

  27. 27 Intenttypes: ExplicitIntents In an Intent, the Dataisspecified by a name and a type NAME: Uniform Resource Identifier (URI) scheme://host:port/path tel:003-232-134-126 content://contacts/people/1 http://www.cs.unibo.it EXAMPLEs

  28. 28 Intenttypes: ExplicitIntents Some actions require data in input to be executed. Use methodsetData(URI) to define the data input of an ImplicitIntent Intent intent=new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:0123456789")); startActivity(intent);

  29. 29 Intenttypes: ExplicitIntents Some actions require data in input to be executed. Use methodsetData(URI) to define the data input of an ImplicitIntent Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("content://contacts/people/1")); startActivity(intent);

  30. 30 Intenttypes: ExplicitIntents Some actions require data in input to be executed. Use methodsetData(URI) to define the data input of an ImplicitIntent Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(”http://www.cs.unibo.it")); startActivity(intent);

  31. 31 Intenttypes: ExplicitIntents In an Intent, the Dataisspecified by a name and a type TYPE: Multipurpose Internet Mail Extensions (MIME) type/subtype EXAMPLEs image/gif image/jpeg image/png text/html text/plain text/css video/mpeg4 … … … …

  32. 32 Intenttypes: ExplicitIntents Some actions require data in input to be executed. Use methodsetType(MIME) to define the data input of an ImplicitIntent Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); startActivity(sendIntent)

  33. 33 Intenttypes: ExplicitIntents Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … Component Name NOT USED • A string containing information about the kind of component that should handle the Intent. • More than one can be specified for an Intent • void addCategory(String) Action Name USED Data USED Category USED Extra Flags

  34. 34 IntentComponents CategoryStringdescribing the kind of component (Activity) thatshouldhandle the Intent.

  35. 35 IntentComponents • Build a new Intent message 2. Specify only the Category of the receiver Activity 3. Fire the Intent through the startActivity() Intent intent=new Intent(); Intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent);

  36. 36 Intenttypes: IntentResolution QUESTION: How can Android know which application to call after an Implicit Intent is fired? ANSWER: Eachapplicationdeclares the Intentisable to handle in the AndroidManifest.xml file If an Intent with Action name ACTION_ECHO is invoked, the Activity is lanched <intent-filter> <action android:name=”ACTION_ECHO” /> </intent-filter>

  37. 37 Three tests must be passed: Actionfield test Categoryfield test Datafield test Intenttypes: IntentResolution The Intentresolutionprocessresolves the Intent-Filterthat can handle a givenIntent(e.g. ACTION_ECHO). If the Intent-filter of Activity A passesall the three test, then itisselected to handle the Intent.

  38. 38 If the filterdoesnotspecifyanyaction FAIL An Intentthatdoesnotspecify an action  SUCCESSasas long as the filtercontainsatleastoneaction. Intenttypes: IntentResolution (ACTION Test): The actionspecified in the Intent must match one of the actionslisted in the filter. <intent-filer … > <action android:name=“com.example.it.ECHO”/> </intent-filter>

  39. 39 If the categoryisnotspecifiedin the Intent AndroidassumesitisCATEGORY_DEFAULT, thus the filter must include thiscategory to handle the Intent. Intenttypes: IntentResolution (CATEGORY Test): Everycategory in the Intent must match a category of the IntentFilter. <intent-filer … > <category android:name=“android.intent.category.DEFAULT”/> </intent-filter>

  40. 40 Intenttypes: IntentResolution (DATA Test): The URI of the intentiscompared with the parts of the URI mentioned in the filter(this part might be incompleted). <intent-filer … > <data android:mimeType=“audio/* android:scheme=“http”/> <data android:mimeType=“video/mpeg android:scheme=“http”/> </intent-filter> • Both URI and MIME-types are compared (4 different sub-cases …)

More Related