0% found this document useful (0 votes)
128 views

Intents

Intents allow communication between application components in Android. They are asynchronous messages that allow components like activities and services to request actions from other components. Intents can be explicit, specifying the exact target component, or implicit, providing information about the action and data without specifying the component. Common actions include viewing web pages, sending emails, and launching phone dialer. Intents contain information like the action, data, category, and extras to describe the operation to be performed.

Uploaded by

Indunil Ramadasa
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)
128 views

Intents

Intents allow communication between application components in Android. They are asynchronous messages that allow components like activities and services to request actions from other components. Intents can be explicit, specifying the exact target component, or implicit, providing information about the action and data without specifying the component. Common actions include viewing web pages, sending emails, and launching phone dialer. Intents contain information like the action, data, category, and extras to describe the operation to be performed.

Uploaded by

Indunil Ramadasa
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/ 6

Android Intents

Intents are asynchronous messages which allow application components to request functionality from
other Android components. Intents allow you to interact with components from the same applications
as well as with components contributed by other applications. For example, an activity can start an
external activity for taking a picture.

An Android Intent is an abstract description of an operation to be performed. It can be used with


startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver
components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate
with a background Service.

For example, let's assume that you have an Activity that needs to launch an email client and sends an
email using your Android device. For this purpose, your Activity would send an ACTION_SEND along
with appropriate chooser, to the Android Intent Resolver. The specified chooser gives the proper
interface for the user to pick how to send your email data.

Intents are objects of the android.content.Intent type. Your code can send them to the Android system
defining the components you are targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.

An intent can contain data via a Bundle. This data can be used by the receiving component.

Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));


email.putExtra(Intent.EXTRA_EMAIL, recipients);
email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
startActivity(Intent.createChooser(email, "Choose an email client
.from..."));

For example, assume that you have an Activity that needs to open URL in a web browser on your
Android device. For this purpose, your Activity will send ACTION_WEB_SEARCH Intent to the Android
Intent Resolver to open given URL in the web browser. The Intent Resolver parses through a list of
Activities and chooses the one that would best match your Intent, in this case, the Web Browser
Activity. The Intent Resolver then passes your web page to the web browser and starts the Web
Browser Activity.
String q = "SLIATE Ratnapura";
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager.QUERY, q);
startActivity(intent);

Above example will search as SLIATE Ratnapura on android search engine and it gives the result of
SLIATE Ratnapura in your an activity.
There are separate mechanisms for delivering intents to each type of component ac vi es,
services, and broadcast receivers.

Context.startActivity()

The Intent object is passed to this method to launch a new activity or get an existing activity to do
something new.

Context.startService()

The Intent object is passed to this method to initiate a service or deliver new instructions to an
ongoing service.

Context.sendBroadcast()

The Intent object is passed to this method to deliver the message to all interested broadcast receivers.

Intent Objects
An Intent object is a bundle of information which is used by the component that receives the
intent as well as information used by the Android system.

An Intent object can contain the following components based on what it is communicating or
going to perform

Action

This is mandatory part of the Intent object and is a string naming the action to be performed
or, in the case of broadcast intents, the action that took place and is being reported. The
action largely determines how the rest of the intent object is structured . The Intent class
defines a number of action constants corresponding to different intents.

The action in an Intent object can be set by the setAction() method and read by getAction().

Data

Adds a data specification to an intent filter. The specification can be just a data type (the
mimeType attribute), just a URI, or both a data type and a URI. A URI is specified by separate
attributes for each of its parts

These attributes that specify the URL format are optional, but also mutually dependent

If a scheme is not specified for the intent filter, all the other URI attributes are ignored.
If a host is not specified for the filter, the port attribute and all the path attributes are
ignored.
The setData() method specifies data only as a URI, setType() specifies it only as a MIME type,
and setDataAndType() specifies it as both a URI and a MIME type. The URI is read by
getData() and the type by getType().

Some examples of action/data pairs are

ACTION_VIEW content://contacts/people/1

Display information about the person whose identifier is "1".


ACTION_DIAL content://contacts/people/1

Display the phone dialer with the person filled in.


ACTION_VIEW tel:123

Display the phone dialer with the given number filled in.
ACTION_DIAL tel:123

Display the phone dialer with the given number filled in.
ACTION_EDIT content://contacts/people/1

Edit information about the person whose identifier is "1".


ACTION_VIEW content://contacts/people/

Display a list of people, which the user can browse through.


ACTION_SET_WALLPAPER

Show settings for choosing wallpaper


ACTION_SYNC

It going to be synchronous the data,Constant Value is android.intent.action.SYNC


ACTION_SYSTEM_TUTORIAL

It will start the platform-defined tutorial(Default tutorial or start up tutorial)


ACTION_TIMEZONE_CHANGED

It intimates when time zone has changed


ACTION_UNINSTALL_PACKAGE

It is used to run default uninstaller


Category

The category is an optional part of Intent object and it's a string containing additional
information about the kind of component that should handle the intent. The addCategory()
method places a category in an Intent object, removeCategory() deletes a category previously
added, and getCategories() gets the set of all categories currently in the object.

Extras

This will be in key-value pairs for additional information that should be delivered to the
component handling the intent. The extras can be set and read using the putExtras() and
getExtras() methods respectively.

The target component which receives the intent can use the getExtras() method to get the
extra data sent by the source component. For example

// Get bundle object at appropriate place in your code


Bundle extras = getIntent().getExtras();

// Extract data using passed keys


String value1 = extras.getString("Key1");
String value2 = extras.getString("Key2");

Flags

These flags are optional part of Intent object and instruct the Android system how to launch
an activity, and how to treat it after it's launched etc.

Types of Intents
There are following two types of intents supported by Android
Intents

Explicit Implicit

Explicit Intents

Explicit intent going to be connected internal world of application,suppose if you wants to


connect one activity to another activity, we can do this quote by explicit intent, below image is
connecting first activity to second activity by clicking button.

These intents designate the target component by its name and they are typically used for
application-internal messages - such as an activity starting a subordinate service or launching
a sister activity. For example

// Explicit Intent by specifying its class name


Intent i = new Intent(this, SecondActivity.class);
// Starts TargetActivity
startActivity(i);

Implicit Intents

Implicit Intent doesn't specifiy the component. In such case, intent provides information of
available components provided by the system that is to be invoked.

For example, you may write the following code to view the webpage.

Intent intent=new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse("http://www.google.com"));

startActivity(intent);

You might also like