Programming with Android: Activities and Intents Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.

Slides:



Advertisements
Similar presentations
Android Application Development A Tutorial Driven Course.
Advertisements

Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
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: Network Operations
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 Informatica – Scienza e Ingegneria 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
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
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
 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.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
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.
Lec 03 Intents Explicit Intents Implicit Intents.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
DKU-MUST Mobile ICT Education Center 10. Activity and Intent.
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.
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
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
Speech Service & client(Activity) 오지영.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
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
MAD.
Activities and Intents
CS371m - Mobile Computing Intents.
Android Programming Lecture 5
Activities and Intents
Android Topics What are Intents? Implicit Intents vs. Explicit Intents
HNDIT2417 Mobile Application Development
Activities and 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 Scienze dell’Informazione 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 – Intents 3 More on Activities: Activity states  Active (or running)  Foreground of the screen (top of the stack)  Paused  Lost focus but still visible  Can be killed by the system in extreme situations  Stopped  Completely obscured by another activity  Killed if memory is needed somewhere else

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 4  An activity lifecycle flows between onCreate and onDestroy  Create, initialize everything you need in onCreate  Destroy everything that is not used anymore, such as background processes, in onDestroy  It is fundamental to save the data used by the application inbetween the state-transitions … More on Activities: Saving resources

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 5 Activities and AndroidManifest.xml  An Android application can be composed of multiple Activities …  Each activity should be declared in the file: AndroidManifest.xml  Add a child element to the tag:

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

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 7 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 in the same or different applications.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 8 Intent Definition  We can think to an “Intent” object as a message containing a bundle of information.  Information of interests for the receiver (e.g. name)  Information of interests for the Android system (e.g. category). Component Name Action Name Data Category Extra Flags Structure of an Intent

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 9 Intent types INTENT TYPES EXPLICIT IMPLICIT The target receiver is specified through the Component Name Used to launch specific Activities The target receiver is specified by data type/names. The system chooses the receiver that matches the request.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 10 Intent Components  We can think to an “Intent” object as a message containing a bundle of information.  Information of interests for the receiver (e.g. data)  Information of interests for the Android system (e.g. category). Component Name Action Name Data Category Extra Flags Component that should handle the intent (i.e. the receiver). It is optional (implicit intent) void setComponent(ComponentName)

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. Intent intent=new Intent(this, SecondActivity.class); startActivity(intent); Intent intent=new Intent(); ComponentName component=new ComponentName(this,SecondActivity.class); intent.setComponent(component); startActivity(intent);

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 12 Intent with Results  Activities can return results (e.g. data)  Sender side: invoke the startActivityForResult()  onActivityResult(int requestCode, int resultCode, Intent data)  startActivityForResult(Intent intent, int requestCode); 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 13  Activities can return results (e.g. data)  Receiver side: invoke the setResult()  void setResult(int resultCode, Intent data) Intent intent=new Intent(); setResult(RESULT_OK, intent); intent.putExtra(”result", resultValue); finish();  The result is delivered to the caller component only after invoking the finish() method! Intent with Results

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 14 Intent types INTENT TYPES EXPLICIT IMPLICIT The target receiver is specified through the Component Name Used to launch specific Activities The target receiver is specified by data type/names. The system chooses the receiver that matches the request.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 15  Implicit Intents: do not name a target (component name is left blank) …  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

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 16 Intent Components  We can think to an “Intent” object as a message containing a bundle of information.  Information of interests for the receiver (e.g. data)  Information of interests for the Android system (e.g. category). Component Name Action Name Data Category Extra Flags 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 17 Intent Components  Predefined 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  Defined by the programmer  it.example.projectpackage.FILL_DATA (package prefix + name action)

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 18 Intent Components  Special actions ( Action NameDescription ACTION_IMAGE_CAPTIONOpen the camera and receive a photo ACTION_VIDEO_CAPTIONOpen 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 19 Intent Components  Example of Implicit Intent that initiates a web search. public void doSearch(String query) { Intent intent =new Intent(Intent.ACTION_SEARCH); Intent.putExtra(SearchManager.QUERY,query); if (intent.resolveActivity(getPackageManager()) !=null) startActivity(intent) }  Example of Implicit Intent that plays a music file. public void playMedia(Uri file) { Intent intent =new Intent(Intent.ACTION_VIEW); if (intent.resolveActivity(getPackageManager()) !=null) startActivity(intent) }

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 20 Intent Components  We can think to an “Intent” object as a message containing a bundle of information.  Information of interests for the receiver (e.g. data)  Information of interests for the Android system (e.g. category). Component Name Action Name Data Category Extra Flags 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)

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 21 Intent Components  Each data is specified by a name and/or type.  name: Uniform Resource Identifier (URI)  scheme://host:port/path tel:// content://contacts/people EXAMPLEs

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 22 Intent Components  Each data is specified by a name and/or type.  type: MIME (Multipurpose Internet Mail Extensions) -type  Composed by two parts: a type and a subtype Image/gifimage/jpegimage/pngimage/tiff text/htmltext/plaintext/javascripttext/css video/mp4video/mpeg4video/quicktimevideo/ogg application/vnd.google-earth.kml+xml EXAMPLEs

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 23 Intent Components  We can think to an “Intent” object as a message containing a bundle of information.  Information of interests for the receiver (e.g. data)  Information of interests for the Android system (e.g. category). Component Name Action Name Data Category Extra Flags A string containing information about the kind of component that should handle the Intent. > 1 can be specified for an Intent void addCategory(String)

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 24 Intent Components  Category: string describing the kind of component 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 25 Intent Components  We can think to an “Intent” object as a message containing a bundle of information.  Information of interests for the receiver (e.g. data)  Information of interests for the Android system (e.g. category). Component Name Action Name Data Category Extra Flags Additional information that should be delivered to the handler(e.g. parameters). Key-value pairs void putExtras() getExtras()

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 26 Intent Components  We can think to an “Intent” object as a message containing a bundle of information.  Information of interests for the receiver (e.g. data)  Information of interests for the Android system (e.g. category). Component Name Action Name Data Category Extra Flags Additional information that instructs Android how to launch an activity, and how to treat it after executed.

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 27 Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(" startActivity(i); Intent types: Implicit Intents Action to performData to perform the action on  Implicit intents are very useful to re-use code and to launch external applications …  More than a component can match the Intent request …  How to define the target component?

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 28  How to declare what intents I'm able to handle? tag in AndroidManifest.xml  How?  If a component creates an Intent with “my.project.ACTION_ECHO ” as action, the corresponding activity will be executed … Intent types: Implicit Intents

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 29  The intent resolution process resolves the Intent- Filter that can handle a given Intent.  Three tests to be passed:  Action field test  Category field test  Data field test  If the Intent-filter passes all the three test, then it is selected to handle the Intent. Intent types: Intent Resolution

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 30  (ACTION Test): The action specified in the Intent must match one of the actions listed in the filter.  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

Luca Bedogni, Marco Di Felice - Programming with Android – Intents 31  (CATEGORY Test): Every category in the Intent must match a category of the filter.  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

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