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

Similar documents
Programming with Android: Activities and Intents. Dipartimento di Informatica Scienza e Ingegneria 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

CS 193A. Multiple Activities and Intents

Multiple Activities. Many apps have multiple activities

stolen from Intents and Intent Filters

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

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

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

Mobile Application Development Android

UNDERSTANDING ACTIVITIES

Activities and Fragments

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

Mobile Development Lecture 8: Intents and Animation

Introduction to Android Multimedia

Using Intents to Launch Activities

Android Programming Lecture 7 9/23/2011

INTENTS android.content.intent

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

Intents and Intent Filters

Intents. Repo:

Activities. Repo:

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

CMSC436: Fall 2013 Week 3 Lab

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

Minds-on: Android. Session 2

Application Fundamentals

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

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

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

Camera and Intent. Elena Fortini

Overview of Activities

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

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

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

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

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

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

BCS3283-Mobile Application Development

CS378 -Mobile Computing. Intents

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

Intents & Intent Filters. codeandroid.org

Understanding Intents and Intent Filters

Developing Android Applications

Android Fundamentals - Part 1

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

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

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

LECTURE NOTES OF APPLICATION ACTIVITIES

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

Android Application Development

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

application components

Understanding Application

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

Android Activities. Akhilesh Tyagi

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

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

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

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

Why using intents. Activity. Screen1 Screen 2

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

Lecture 2 Android SDK

DROIDS ON FIRE. Adrián

MOBILE APPLICATION DEVELOPMENT LECTURE 10 SERVICES IMRAN IHSAN ASSISTANT PROFESSOR

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

Introduction to Android Development

App Development for Android. Prabhaker Matet

Lab 1: Getting Started With Android Programming

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

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

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

Android Help. Section 8. Eric Xiao

Android: Intents, Menus, Reflection, and ListViews

COSC 3P97 Mobile Computing

Applications. Marco Ronchetti Università degli Studi di Trento

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

States of Activities. Active Pause Stop Inactive

Android Application Development 101. Jason Chen Google I/O 2008

Tizen Ver2.2 Application API Example

Android Services & Local IPC: The Command Processor Pattern (Part 1)

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Android Intents. Notes are based on: Android Developers

Ariadnima - Android Component Flow Reconstruction and Visualization

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

Android Ecosystem and. Revised v4presenter. What s New

Mobile Computing. Introduction to Android

EMBEDDED SYSTEMS PROGRAMMING Android Services

Another difference is that the kernel includes only the suspend to memory mechanism, and not the suspend to hard disk, which is used on PCs.

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

Security Philosophy. Humans have difficulty understanding risk

Introduction to Android

INTRODUCTION TO ANDROID

Terms: MediaPlayer, VideoView, MediaController,

FRAGMENTS.

CS434/534: Topics in Networked (Networking) Systems

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

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

COLLEGE OF ENGINEERING, NASHIK-4

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Transcription:

Programming with Android: Intents Luca Bedogni Dipartimento di Scienze dell Informazione 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 - Programming with Android Intents 2

More on Activities: Activity states Active (or running) Foreground of the screen (top of the stack) Paused Stopped Lost focus but still visible Can be killed by the system in extreme situations Completely obscured by another activity Killed if memory is needed somewhere else Luca Bedogni - Programming with Android Intents 3

More on Activities: Saving resources 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 Luca Bedogni - Programming with Android Intents 4

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 <application> tag: <application> <activity android:name=".myactivity" /> <activity android:name=.secondactivity" /> </application> Luca Bedogni - Programming with Android Intents 5

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 - Programming with Android Intents 6

Intent Definition Intent: facility for late run-time binding between components in 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 this data Reuse already installed applications and components Luca Bedogni - Programming with Android Intents 7

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 Structure of an Intent Data Category Extra Flags Luca Bedogni - Programming with Android Intents 8

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 - Programming with Android Intents 9

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 - Programming with Android Intents 10

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 - Programming with Android Intents 11

Intent with Results Activities can return results (e.g. data) Sender side: invoke the startactivityforresult() q onactivityresult(int requestcode, int resultcode, Intent data) q 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 - Programming with Android Intents 12

Intent with Results Activities can return results (e.g. data) Receiver side: invoke the setresult() q 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! Luca Bedogni - Programming with Android Intents 13

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 - Programming with Android Intents 14

Intent types: Implicit Intents 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) Luca Bedogni - Programming with Android Intents 15

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 A string naming the action to be performed. Pre-defined, or can be specified by the programmer. Extra Flags void setaction(string) Luca Bedogni - Programming with Android Intents 16

Intent Components Predefined actions (http://developer.android.com/reference/android/content/intent.html) Action Name ACTION_EDIT ACTION_MAIN ACTION_PICK ACTION_VIEW ACTION_SEARCH Description Display data to edit Start as a main entry point, does not expect to receive data. Pick an item from the data, returning what was selected. Display the data to the user Perform a search Defined by the programmer it.example.projectpackage.fill_data (package prefix + name action) Luca Bedogni - Programming with Android Intents 17

Intent Components Special actions (http://developer.android.com/reference/android/content/intent.html) Action Name ACTION_IMAGE_CAPTION ACTION_VIDEO_CAPTION 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 - Programming with Android Intents 18

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 - Programming with Android Intents 19

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 Data passed from the caller to the called Component. Def. of the data (URI) and Type of the data (MIME type) Extra Flags void setdata(uri) void settype(string) Luca Bedogni - Programming with Android Intents 20

Intent Components Each data is specified by a name and/or type. name: Uniform Resource Identifier (URI) scheme://host:port/path EXAMPLEs tel://003-232-234-678 content://contacts/people http://www.cs.unibo.it/ Luca Bedogni - Programming with Android Intents 21

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 EXAMPLEs Image/gif image/jpeg image/png image/tiff text/html text/plain text/javascript text/css video/mp4 video/mpeg4 video/quicktime video/ogg application/vnd.google-earth.kml+xml Luca Bedogni - Programming with Android Intents 22

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 A string containing information about the kind of component that should handle the Intent. > 1 can be specified for an Intent Extra Flags void addcategory(string) Luca Bedogni - Programming with Android Intents 23

Intent Components Category: string describing the kind of component 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 - Programming with Android Intents 24

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 Additional information that should be delivered to the handler(e.g. parameters). Key-value pairs Extra Flags void putextras() getextras() Luca Bedogni - 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 instructs Android how to launch an activity, and how to treat it after executed. Luca Bedogni - Programming with Android Intents 26

Intent types: Implicit Intents Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://informatica.unibo.it")); startactivity(i); Action to perform Data 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 - Programming with Android Intents 27

Intent types: Implicit Intents How to declare which intents I'm able to handle? <intent-filter> tag in AndroidManifest.xml How? <intent-filter> <action android:name= my.project.action_echo /> </intent-filter> If a component creates an Intent with my.project.action_echo as action, the corresponding activity will be executed Luca Bedogni - Programming with Android Intents 28

The intent resolution process resolves the Intent- Filter that can handle a given Intent. Three tests to be passed: Intent types: Intent Resolution 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. Luca Bedogni - Programming with Android Intents 29

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 - Programming with Android Intents 30

Intent types: Intent Resolution (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-filer > <category android:name= android.intent.category.default /> </intent-filter> Luca Bedogni - Programming with Android Intents 31

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 incomplete). <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 - Programming with Android Intents 32