Hello Everyone!
This article is based on Intents in Android. Here we are discussing about intents, their types, basic intent syntax, intent filter and intent object.
From university exam perspective, intents in android is an important topic and has been asked few times along with Android Architecture and Android Components.
Intents
are message passing mechanisms that are used within an application as well as
between various applications. Intents
in general are used to navigate users among various activities within same
application or in a different application.
A
classic example of intent is using intent between two different applications.
Suppose a location of a place is shared on WhatsApp. When the user clicks on
the location, he/she is then transferred into google map application where they
can see the location on the map.
Intents are not only restricted into moving/switching from one application to other but can also be used to -
(1) Explicitly start an activity or a service by using its class name
(2)
Start an activity or a service to perform some action with some data
(3) Broadcast about some event that has occurred.
Thus,
we can say that intent acts as a connecting unit between the different
applications installed on the device.
Intents
in android are of two types -
(A)
Implicit intents
(B)
Explicit intents
A)
Implicit intents-
It
does not specify any specific component. It just declares a general action to
perform and allows components from other application to handle it.
We
can use implicit intent where we cannot mention any target activity.
For
example, we use implicit intent to open an URL. Here we do not specify which
application will open this URL. It depends on the system which application will
handle this request.
Implicit
intent uses the following syntax -
Intent intent =new Intent (Intent.ACTION_VIEW);
Intent.setData(Uri.parse(“https://www.google.com”));
startActivity(intent);
B)
Explicit intents-
We
use explicit intents when we explicitly name the class of the target activity
that will handle the intent. Generally explicit intents are used within same
application as the developer knows the names of classes present in that
application.
Explicit
intent can be used to take user to some different activity when the user clicks
on some button or other widget.
Explicit intent uses the following syntax -
Intent intent = new Intent (getApplicationContext(), AnotherActivity.class);
startActivity(intent);
Intent
Filter -
An
intent filter is an expression in an app's manifest file that specifies the
type of intents that component may receive. By declaring an intent filter for
any activity, we can allow other applications to directly start the activity.
If intent filters are not declared then the application can be only started
with an explicit intent.
Intent Object -
An
intent object carries information that the android system uses to determine
which component to start (such as the exact component name or component
category that should receive the intent), plus information that the recipient
component uses in order to properly perform the action (such as the action to
take and the data to act upon).
The
various fields that the intent object consists are-
(A)
Component name
(B)
Action
(C)
Data
(D)
Category
(E)
Extra
(F)
Flags
A)
Component name -
It
is used in explicit intents which specifies the name of the activity. Without a
component name, the intent is implicit. The name must be a proper class name,
including the package name of the app.
B)
Action -
It
is a string that specifies a generic action to perform. The action determines how
the rest of the intent is structured particularly the information that is
contained in the data and extras.
C)
Data -
It
is necessary to specify type (MIME) type of data. The type of data supplied is
generally dictated by the intents action.
D)
Category -
It
is a string containing additional information about the kind of component that
should handle the intent. It is not compulsory to be included.
E)
Extra -
They
are key value pairs which carry additional information required to accomplish
the requested action.
F)
Flags -
They
function as metadata tor the intent. The flags may instruct the Android system
how to launch an activity and how to treat it after it is the launched.
Comments
Post a Comment