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

Slides:



Advertisements
Similar presentations
Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Advertisements

Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Programming with Android: SDK install and initial setup Luca Bedogni Marco Di Felice Dipartimento di Scienze dellInformazione Università di Bologna.
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dellInformazione Università di Bologna.
Programming with Android: Activities and Intents
Programming with Android: Activities
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Programming with Android: System Architecture
CE881: Mobile and Social Application Programming Simon M. Lucas Quiz, Walkthrough, Exercise, Lifecycles, Intents.
Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Programming with Android: Activities and Intents Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Programming with Android: Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Programming with Android: SDK install and initial setup Luca Bedogni Marco Di Felice Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna.
Android Projects Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Programming with Android: Activities
Android 02: Activities David Meredith
Intents.
Manifest File, Intents, and Multiple Activities. Manifest File.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya.
Intent An Intent describes the operation to be performed. Intents are used to start an activity using either of the following methods – Context.startActivity()
Programming with Android: Android Fragments Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Android Middleware Bo Pang
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Chapter 5: Investigate! Lists, Arrays, and Web Browsers.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
CS378 - Mobile Computing Intents.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Overview of Android Application Development
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Lec 03 Intents Explicit Intents Implicit Intents.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (
Introducing Intents Intents Bind application components and navigate between them Transform device into collection of interconnected systems Creating a.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
Intents 1 CS440. Intents  Message passing mechanism  Most common uses:  starting an Activity (open an , contact, etc.)  starting an Activity.
Android Intents Nasrullah. Using the application context You use the application context to access settings and resources shared across multiple activity.
Lecture 2: Android Concepts
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Android Mobile Application Development
Mobile Applications (Android Programming)
Lecture 2: Android Concepts
Programming with Android:
Android 5: Interacting with Other Apps
Linking Activities using Intents
Activities and Intents
Mobile Application Development BSCS-7 Lecture # 3
CS371m - Mobile Computing Intents.
Android Programming Lecture 5
Activities and Intents
Android Topics What are Intents? Implicit Intents vs. Explicit Intents
Activities and Intents
Objects First with Java
Activities and Intents
It is used to Start an Activity Start a Service Deliver a Broadcast
Lecture 2: Android Concepts
Objects First with Java
Android Development Tools
Presentation transcript:

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

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 2 Outline Intent with results: Sender side Intent-Resolution process Handling implicit Intents Handling Explicit Intents Intent description What is an intent? Intent with results: Receiver side

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 3 Android Applications Design Developing an Android Application means using in a proper way the Android basic components … Activity Layout Views Fragment Intent Service Broadcast Receiver Broadcast Receiver Content Providers Content Providers

Luca Bedogni, Marco Di Felice - Programming with Android – Resources 4 Android Applications Design Activity Service Broadcast Receiver Broadcast Receiver Intent Developing an Android Application means using in a proper way the Android basic components …

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 5 More on Activities: Activity states In most cases, an Android Application is composed of multiple Activities, not just one … LOGIN PASSWOR D Login marco ********** Welcome Marco! Go to Next Activity Activity 1 Activity 2

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 6 Each Activity has its own:  Java implementation  XML Layout file  Lifecycle with dif fere nt states  XML Tag in AndroidManifest.xml More on Activities: Saving resources MyActivity.java activity_main.xml ACTIVEPAUSEDSTOPPED

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 7 Activities and AndroidManifest.xml 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); }  Each activity has its Java class and layout file.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 8 Intent Definition  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.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 9 Intent Definition Component Name Action Name Data Category Extra Flags Structure of an Intent We can think to an Intent object as a message containing a bundle of information.  Intent is sent from current Activity to a receiver Activity which is then activated and executed.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 10 Intent types INTENT TYPES IMPLICIT The target receiver is specified through the Component Name Used to launch specific Activities EXPLICIT The target receiver is specified through the actions to be executed. The system chooses the receiver that matches the request.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 11 Intent types: Explicit Intents Explicit Intent  Specify the name of the Activity that will handle the intent. Component Name Action Name Data Category Extra Flags Structure of an Intent USED NOT USED  Used to control the execution flow between Activities belonging to the same Android application.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 12 Intent types: Explicit Intents 1.Build a new Intent message 2. Specify the Activity who will receive the Intent 3. Fire the Intent through the startActivity() Intent intent=new Intent(this, SecondActivity.class); startActivity(intent); NAME OF THE ACTIVITY TO START

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 13 Intent types: Explicit Intents 1.Build a new Intent message 2. Specify the Activity who will receive the Intent 3. Fire the Intent through the startActivity() Intent intent=new Intent(); ComponentName component=new ComponentName(this,SecondActivity.class); intent.setComponent(component); startActivity(intent); ALTERNATIVE code

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 14 Intent types: Explicit Intents (OPTIONAL) Insert parameters to be sent to the called Activity in the Extra field of the Intent. Intent intent=new Intent(this, SecondActivity.class); intent.putExtra(“myValue”,5); startActivity(intent); intent.putExtra(“KEY”, VALUE); Set an argument named “MyValue” and equal to 5.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 15 Intent types: Explicit Intents (OPTIONAL) From the called Activity, retrieve the parameters inserted from the calling Activity intent.getExtras().getInt(“myValue”); intent.getExtras().getString(“myString”); intent.getExtras().getBoolean(“myBoolean”); intent.getExtras().getTYPE(“KEY”); Get an argument of type int with key equal to “myValue”

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 16 Intent types: Explicit Intents 1.IMPLEMENT method onActivityResult(int requestCode, int resultCode, Intent data) 2.INVOKE method startActivityForResult(Intent intent, int requestCode) Some Activities can return results to the caller Activity SENDER SIDE 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

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 17 Intent types: Explicit Intents 1.IMPLEMENT method setResult(int resultCode, Intent data) 2.SET results on the Extra field of the Intent Some Activities can return results to the caller Activity RECEIVER SIDE Intent intent=new Intent(); setResult(RESULT_OK, intent); intent.putExtra(”result", resultValue); finish(); The result is delivered only after invoking the finish() method!

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 18 Intent types INTENT TYPES EXPLICIT The target receiver is specified through the component Name Used to launch specific Activities The target receiver is specified through the actions to be executed. The system chooses the receiver that matches the request. IMPLICIT

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 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) Intent types: Implicit Intents Implicit Intents  do not name a target (component name is left blank) but an intention of what to do …

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 20 Intent types: Explicit Intents Component Name Action Name Data Category Extra Flags Structure of an Intent NOT USED USED  Used to control the execution flow between Activities belonging to DIFFERENT Android applications. Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … USED

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 21 Intent types: Explicit Intents Component Name Action Name Data Category Extra Flags NOT USED USED Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … USED  A string naming the action to be performed.  Pre-defined, or can be specified by the programmer.  void setAction(String)

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 22 Intent types: Explicit Intents Special actions ( Action NameDescription ACTION_IMAGE_CAPTUREOpen the camera and receive a photo ACTION_VIDEO_CAPTUREOpen the camera and receive a video ACTION_DIALOpen the phone app and dial a phone number ACTION_SENDTOSend an ( data contained in the extra) ACTION_SETTINGSOpen the system setting ACTION_WIRELESS_SETTINGSOpen the system setting of the wireless interfaces ACTION_DISPLAY_SETTINGSOpen the system setting of the display

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 23 Intent types: Explicit Intents Generic actions ( Action NameDescription ACTION_EDITDisplay data to edit ACTION_MAINStart as a main entry point, does not expect to receive data. ACTION_PICKPick an item from the data, returning what was selected. ACTION_VIEWDisplay the data to the user ACTION_SEARCHPerform a search  Action Defined by the programmer it.example.projectpackage.FILL_DATA (package prefix + name action)

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 24 Intent types: Explicit Intents 1.Build a new Intent message 2. Specify only the Action you want to perform 3. Fire the Intent through the startActivity() Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivity(intent); ACTION NAME

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 25 Intent types: Explicit Intents 1.Build a new Intent message 2.Specify only the Action you want to perform 3.Fire the Intent through the startActivity() 4.Verify whether the Intent can be handled Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); }

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 26 Intent types: Explicit Intents Component Name Action Name Data Category Extra Flags NOT USED USED Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … USED  Data passed from the caller to the called Component.  Def. of the data (URI) and Type of the data (MIME type) 1.void setData(Uri) 2.void setType(String)

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 27 Intent types: Explicit Intents In an Intent, the Data is specified by a name and a type NAME: Uniform Resource Identifier (URI) scheme://host:port/path tel: content://contacts/people/1 EXAMPLEs

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 28 Intent types: Explicit Intents Some actions require data in input to be executed. Intent intent=new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel: ")); startActivity(intent); Use method setData(URI) to define the data input of an Implicit Intent

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 29 Intent types: Explicit Intents Some actions require data in input to be executed. Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(" content://contacts/people /1")); startActivity(intent); Use method setData(URI) to define the data input of an Implicit Intent

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 30 Intent types: Explicit Intents Some actions require data in input to be executed. Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(” ")); startActivity(intent); Use method setData(URI) to define the data input of an Implicit Intent

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 31 Intent types: Explicit Intents In an Intent, the Data is specified by a name and a type TYPE: Multipurpose Internet Mail Extensions (MIME) type/subtype image/gif image/jpeg image/png text/html text/plain text/css video/mpeg4 … … … … EXAMPLEs

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 32 Intent types: Explicit Intents Some actions require data in input to be executed. Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); startActivity(sendIntent) Use method setType(MIME) to define the data input of an Implicit Intent

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 33 Intent types: Explicit Intents Component Name Action Name Data Category Extra Flags NOT USED USED Implicit Intents  do not name a target (component name is left blank) but an intention of what to do … 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)

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 34 Intent Components Category  String describing the kind of component (Activity) that should handle the Intent. Category NameDescription CATEGORY_HOMEThe activity displays the HOME screen. CATEGORY_LAUNCHERThe activity is listed in the top-level application launcher, and can be displayed. CATEGORY_PREFERENCEThe activity is a preference panel. CATEGORY_BROWSABLEThe activity can be invoked by the browser to display data referenced by a link.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 35 Intent Components 1.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);

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 36 Intent types: Intent Resolution QUESTION: How can Android know which application to call after an Implicit Intent is fired? ANSWER: Each application declares the Intent is able to handle in the AndroidManifest.xml file If an Intent with Action name ACTION_ECHO is invoked, the Activity is lanched

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 37 Three tests must be passed: 1. Action field test 2. Category field test 3. Data field test Three tests must be passed: 1. Action field test 2. Category field test 3. Data field test Intent types: Intent Resolution The Intent resolution process resolves the Intent-Filter that can handle a given Intent (e.g. ACTION_ECHO). If the Intent-filter of Activity A passes all the three test, then it is selected to handle the Intent. If the Intent-filter of Activity A passes all the three test, then it is selected to handle the Intent.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 38  If the filter does not specify any action  FAIL  An Intent that does not specify an action  SUCCESS as as long as the filter contains at least one action. Intent types: Intent Resolution ( ACTION Test): The action specified in the Intent must match one of the actions listed in the filter. ( ACTION Test): The action specified in the Intent must match one of the actions listed in the filter.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 39  If the category is not specified in the Intent  Android assumes it is CATEGORY_DEFAULT, thus the filter must include this category to handle the Intent. Intent types: Intent Resolution (CATEGORY Test): Every category in the Intent must match a category of the Intent Filter.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 40 Intent types: Intent Resolution  Both URI and MIME-types are compared (4 different sub-cases …) (DATA Test): The URI of the intent is compared with the parts of the URI mentioned in the filter (this part might be incompleted).