0% found this document useful (0 votes)
12 views34 pages

06 Intents

Intents. pemrograman mobile

Uploaded by

Dini Ramadani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views34 pages

06 Intents

Intents. pemrograman mobile

Uploaded by

Dini Ramadani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

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
Ø 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 - 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
Data
Structure
of an Intent Category
Extra Flags

Luca Bedogni - Programming with Android – Intents 8


Intent types

INTENT TYPES

EXPLICIT IMPLICIT

The target receiver is specified The target receiver is specified


through the Component Name by data type/names.

Used to launch specific Activities 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 Component that should handle


Action Name the intent (i.e. the receiver).

Data It is optional (implicit intent)


Category
void setComponent(ComponentName)
Extra Flags

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 The target receiver is specified


through the Component Name by data type/names.

Used to launch specific Activities 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 A string naming the action to


Action Name be performed.

Data Pre-defined, or can be


Category specified by the programmer.
Extra Flags void setAction(String)

Luca Bedogni - Programming with Android – Intents 16


Intent Components

Ø Activity A fires an Intent

Ø Android looks for capable


activities
Ø By looking at the manifests

Ø When one is found, it is


called

Luca Bedogni - Programming with Android – Intents 17


Intent Components

Ø Predefined actions (http://developer.android.com/reference/android/content/Intent.html)


Action Name Description
ACTION_EDIT Display data to edit
ACTION_MAIN Start as a main entry point, does not expect to receive
data.
ACTION_PICK Pick an item from the data, returning what was selected.
ACTION_VIEW Display the data to the user
ACTION_SEARCH Perform a search

Ø Defined by the programmer


Ø it.example.projectpackage.FILL_DATA (package prefix + name action)

Luca Bedogni - Programming with Android – Intents 18


Intent Components

Ø Special actions (http://developer.android.com/reference/android/content/Intent.html)


Action Name Description
ACTION_IMAGE_CAPTION Open the camera and receive a photo
ACTION_VIDEO_CAPTION Open the camera and receive a video

ACTION_DIAL Open the phone app and dial a phone number


ACTION_SENDTO Send an email (email data contained in the extra)
ACTION_SETTINGS Open the system setting
ACTION_WIRELESS_SETTINGS Open the system setting of the wireless
interfaces
ACTION_DISPLAY_SETTINGS Open the system setting of the display

Luca Bedogni - 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 - 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 Data passed from the caller to


Action Name the called Component.

Data Def. of the data (URI) and


Category Type of the data (MIME type)
Extra Flags void setData(Uri)
void setType(String)
Luca Bedogni - 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
EXAMPLEs

tel://003-232-234-678
content://contacts/people
http://www.cs.unibo.it/

Luca Bedogni - 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
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 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 A string containing information


about the kind of component
Action Name that should handle the Intent.
Data
> 1 can be specified for an Intent
Category
Extra Flags void addCategory(String)

Luca Bedogni - Programming with Android – Intents 24


Intent Components

Ø Category: string describing the kind of component that


should handle the intent.

Category Name Description


CATEGORY_HOME The activity displays the HOME screen.
CATEGORY_LAUNCHER The activity is listed in the top-level application
launcher, and can be displayed.
CATEGORY_PREFERENCE The activity is a preference panel.
CATEGORY_BROWSABLE The activity can be invoked by the browser to
display data referenced by a link.

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 Additional information that


should be delivered to the
Action Name handler(e.g. parameters).
Data
Key-value pairs
Category
Extra Flags void putExtras() getExtras()

Luca Bedogni - 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 Additional information that
instructs Android how to launch
Category an activity, and how to treat it
Extra Flags after executed.

Luca Bedogni - Programming with Android – Intents 27


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 28


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 29


Intent types: Intent Resolution

Ø 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.
Luca Bedogni - Programming with Android – Intents 30
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 31
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 32


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 33


Common Intents

Ø Setting an Alarm – ACTION_SET_ALARM


Ø Timer – ACTION_SET_TIMER
Ø Calendar Events – ACTION_INSERT and data
Ø Camera – ACTION_IMAGE_CAPTURE /
ACTION_VIDEO_CAPTURE
Ø Email – ACTION_SEND / ACTION_SENDTO
Ø Files – ACTION_GET_CONTENT
Ø Various meanings – ACTION_VIEW
More at: https://developer.android.com/guide/components/intents-common.html
Luca Bedogni - Programming with Android – Intents 34

You might also like