Programming with Android: Activities and Intents. Dipartimento di Informatica Scienza e Ingegneria Università di Bologna

Similar documents
Programming with Android: Intents. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

Intents. Repo:

The Intent Class Starting Activities with Intents. Explicit Activation Implicit Activation via Intent resolution

Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department. Mobile Computing ECOM Eng. Wafaa Audah.

Mobile Application Development Android

The Intent Class Starting Activities with Intents. Explicit Activation Implicit Activation via Intent resolution

CS 193A. Multiple Activities and Intents

Intents. 18 December 2018 Lecture Dec 2018 SE 435: Development in the Android Environment 1

stolen from Intents and Intent Filters

Multiple Activities. Many apps have multiple activities

Introduction to Android Multimedia

Using Intents to Launch Activities

INTENTS android.content.intent

Workshop. 1. Create a simple Intent (Page 1 2) Launch a Camera for Photo Taking

Overview. Lecture: Implicit Calling via Share Implicit Receiving via Share Launch Telephone Launch Settings Homework

Android. Operating System and Architecture. Android. Screens. Main features

Activities and Fragments

Intents and Intent Filters

CS378 -Mobile Computing. Intents

Mobile Development Lecture 8: Intents and Animation

Android Programming Lecture 7 9/23/2011

Developing Android Applications

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

CMSC436: Fall 2013 Week 3 Lab

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

Mobile Programming Practice Explicit intent Implicit intent Intent filter Lab#4 PA #1

Software Practice 3 Today s lecture Today s Task Porting Android App. in real device

Intents & Intent Filters. codeandroid.org

UNDERSTANDING ACTIVITIES

Camera and Intent. Elena Fortini

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

Minds-on: Android. Session 2

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Android System Architecture. Android Application Fundamentals. Applications in Android. Apps in the Android OS. Program Model 8/31/2015

Questions and Answers. Q.1) Which of the following is the most ^aeuroeresource hungry ^aeuroepart of dealing with activities on android?

Activities. Repo:

Introduction to Android Development

Overview of Activities

Programming with Android: SDK install and initial setup. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android Help. Section 8. Eric Xiao

Terms: MediaPlayer, VideoView, MediaController,

Services. service: A background task used by an app.

Understanding Application

LECTURE NOTES OF APPLICATION ACTIVITIES

Programming with Android: System Architecture. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

Programming with Android: SDK install and initial setup. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

BCS3283-Mobile Application Development

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Understanding Intents and Intent Filters

Application Fundamentals

Midterm Examination. CSCE 4623 (Fall 2017) October 20, 2017

Outline. Admin. Example: TipCalc. Android: Event Handler Blocking, Android Inter-Thread, Process Communications. Recap: Android UI App Basic Concepts

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Android: Intents, Menus, Reflection, and ListViews

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

M.C.A. Semester V Subject: - Mobile Computing (650003) Week : 2

Using Libraries, Text-to-Speech, Camera. Lecture 12

Android Application Development

Android Fundamentals - Part 1

CS434/534: Topics in Networked (Networking) Systems

Android Activities. Akhilesh Tyagi

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Introduction To JAVA Programming Language

Programmation Mobile Android Master CCI

Android Programming Lecture 8: Activities, Odds & Ends, Two New Views 9/28/2011

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Android User Interface Android Smartphone Programming. Outline University of Freiburg

CS 4330/5390: Mobile Application Development Exam 1

Ariadnima - Android Component Flow Reconstruction and Visualization

Writing Efficient Drive Apps for Android. Claudio Cherubino / Alain Vongsouvanh Google Drive Developer Relations

Interworking Guide for Android Samsung Apps

Android permissions Defining and using permissions Component permissions and related APIs

Android Intents. Notes are based on: Android Developers

THE CATHOLIC UNIVERSITY OF EASTERN AFRICA A. M. E. C. E. A

Android User Interface

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Security Philosophy. Humans have difficulty understanding risk

Lab 1: Getting Started With Android Programming

Applications. Marco Ronchetti Università degli Studi di Trento

PAPER ON ANDROID ESWAR COLLEGE OF ENGINEERING SUBMITTED BY:

Collusive Data Leak and More: Large-scale Threat Analysis of Inter-app Communications. Amiangshu Bosu, Fang Liu, Danfeng (Daphne) Yao, & Gang Wang

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

SHWETANK KUMAR GUPTA Only For Education Purpose

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

<uses-permission android:name="android.permission.internet"/>

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

DROIDS ON FIRE. Adrián

Basic UI elements: Android Buttons (Basics) Marco Ronchetti Università degli Studi di Trento

Tizen Ver2.2 Application API Example

App Development for Android. Prabhaker Matet

Why using intents. Activity. Screen1 Screen 2

INTRODUCTION TO ANDROID

Mobile Programming Lecture 9. Bound Services, Location, Sensors, IntentFilter

External Services. CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C. Champion

Android AND-401. Android Application Development. Download Full Version :

Programming with Android: Animations, Menu, Toast and Dialogs. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android Basics. Android UI Architecture. Android UI 1

Transcription:

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 Handling implicit Intents Intent-Resolution process Intent with results: Sender side Intent with results: Receiver side Luca Bedogni, Marco Di Felice - Programming with Android Resources 2

Android Applications Design Developing an Android Application means using in a proper way the Android basic components Activity Fragment Intent Service Layout Views Content Providers Broadcast Receiver 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 Intent Service Activity Intent Broadcast Receiver Luca Bedogni, Marco Di Felice - Programming with Android Resources 4

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

More on Activities: Saving resources Each Activity has its own: ² Java implementation ² XML Layout file ² Lifecycle with different states MyActivity.java activity_main.xml ACTIVE PAUSED STOPPED ² XML Tag in AndroidManifest.xml <application> <activity android:name=".myactivity" /> </application> 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 Intent: facility for late run-time binding between components of the same or different applications. ² 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 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. Structure of an Intent Component Name Action Name Data Category Extra Flags ² 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 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 through the actions to be executed. The system chooses the receiver that matches the request. Luca Bedogni, Marco Di Felice - Programming with Android Intents 10

Explicit Intent à Specify the name of the Activity that will handle the intent. Structure of an Intent Component Name Action Name Data Category Extra Flags USED NOT USED NOT 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 11

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

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(); ALTERNATIVE code ComponentName component=new ComponentName(this,SecondActivity.class); intent.setcomponent(component); startactivity(intent); Luca Bedogni, Marco Di Felice - Programming with Android Intents 13

(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); Luca Bedogni, Marco Di Felice - Programming with Android Intents 14

(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 ); Luca Bedogni, Marco Di Felice - Programming with Android Intents 15

Some Activities can return results to the caller Activity SENDER SIDE 1. IMPLEMENT method onactivityresult(int requestcode, int resultcode, Intent data) 2. INVOKE method 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 16

Some Activities can return results to the caller Activity RECEIVER SIDE 1. IMPLEMENT method setresult(int resultcode, Intent data) 2. 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! Luca Bedogni, Marco Di Felice - Programming with Android Intents 17

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 through the actions to be executed. The system chooses the receiver that matches the request. Luca Bedogni, Marco Di Felice - Programming with Android Intents 18

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

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

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

Special actions (http://developer.android.com/reference/android/content/intent.html) Action Name ACTION_IMAGE_CAPTURE ACTION_VIDEO_CAPTURE ACTION_DIAL ACTION_SENDTO ACTION_SETTINGS ACTION_WIRELESS_SETTINGS ACTION_DISPLAY_SETTINGS Description Open the camera and receive a photo Open the camera and receive a video Open the phone app and dial a phone number Send an email (email data contained in the extra) Open the system setting Open the system setting of the wireless interfaces Open the system setting of the display Luca Bedogni, Marco Di Felice - Programming with Android Intents 22

Generic actions (http://developer.android.com/reference/android/content/intent.html) Action Name ACTION_EDIT ACTION_MAIN Description Display data to edit Start as a main entry point, does not expect to receive data. ACTION_PICK ACTION_VIEW ACTION_SEARCH Pick an item from the data, returning what was selected. Display the data to the user Perform 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 23

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

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 25

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

In an Intent, the Data is specified 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 Luca Bedogni, Marco Di Felice - Programming with Android Intents 27

Some actions require data in input to be executed. Use method setdata(uri) to define the data input of an Implicit Intent Intent intent=new Intent(Intent.ACTION_DIAL); intent.setdata(uri.parse("tel:0123456789")); startactivity(intent); Luca Bedogni, Marco Di Felice - Programming with Android Intents 28

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

Some actions require data in input to be executed. Use method setdata(uri) to define the data input of an Implicit Intent Intent intent=new Intent(Intent.ACTION_VIEW); intent.setdata(uri.parse( http://www.cs.unibo.it")); startactivity(intent); Luca Bedogni, Marco Di Felice - Programming with Android Intents 30

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 text/html text/plain video/mpeg4 EXAMPLEs image/png text/css Luca Bedogni, Marco Di Felice - Programming with Android Intents 31

Some actions require data in input to be executed. Use method settype(mime) to define the data input of an Implicit Intent Intent sendintent = new Intent(); sendintent.setaction(intent.action_send); sendintent.putextra(intent.extra_text, textmessage); sendintent.settype(http.plain_text_type); startactivity(sendintent) Luca Bedogni, Marco Di Felice - Programming with Android Intents 32

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

Intent Components Category à String describing the kind of component (Activity) that should handle the Intent. Category Name CATEGORY_HOME CATEGORY_LAUNCHER CATEGORY_PREFERENCE CATEGORY_BROWSABLE Description The activity displays the HOME screen. The activity is listed in the top-level application launcher, and can be displayed. The activity is a preference panel. The activity can be invoked by the browser to display data referenced by a link. Luca Bedogni, Marco Di Felice - Programming with Android Intents 34

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 35

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 <intent- filter> <action android:name= ACTION_ECHO /> </intent- filter> Luca Bedogni, Marco Di Felice - Programming with Android Intents 36

Intent types: Intent Resolution The Intent resolution process resolves the Intent-Filter that can handle a given Intent (e.g. ACTION_ECHO). Three tests must be passed: 1. Action field test 2. Category field test 3. Data field test 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 37

Intent types: Intent Resolution (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- filer > <action android:name= com.example.it.echo /> </intent- filter> Luca Bedogni, Marco Di Felice - Programming with Android Intents 38

Intent types: Intent Resolution (CATEGORY Test): Every category in the Intent must match a category of the Intent 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- filer > <category android:name= android.intent.category.default /> </intent- filter> Luca Bedogni, Marco Di Felice - Programming with Android Intents 39

Intent types: Intent Resolution (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- 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 ) Luca Bedogni, Marco Di Felice - Programming with Android Intents 40