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

Intent

Uploaded by

Prakash Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Intent

Uploaded by

Prakash Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Android Intent is the message that is passed between components such as activities,

content providers, broadcast receivers, services etc.

Android intents are mainly used to:

o Start the service


o Launch an activity
o Display a web page
o Display a list of contacts
o Broadcast a message
o Dial a phone call etc.

Type of Intents:

1.Implicit Intent

2.Explicit Intent

Implicit Intent:

An implicit Intent is an instance of the Intent class. Implicit Intent doesn’t specify the
component in the app. In such a case, intent provides information on available components
provided by the system that is to be invoked.
Fields used by an implicit Intent:

• Intent action
• Intent category
• data type
Note: Intent actions, categories, and data types are used both by the Intent object you
create in your sending Activity, as well as in the Intent filters you define in
the AndroidManifest.xml file for the receiving Activity.

Intent action:These are used as constants of intent class. It begin with the
word ACTION_.
Eg:ACTION_VIEW, which you use when you have some information that an Activity can
show to the user.

Intent category, which provides additional information about the category of


component that should handle the Intent. Intent categories are optional, and you
can add more than one category to an Intent. Intent categories are also defined
as constants in the Intent class and begin with the word CATEGORY_. You can add
categories to the Intent with the addCategory() method.

The data type, which indicates the MIME type of data the Activity should operate
on. Usually, the data type is inferred from the URI in the Intent data field, but you
can also explicitly define the data type with the setType() method.

Once you have an Intent object you can add other information (category, data,
extras) with the various Intent methods. For example, this code creates an
implicit Intent object, sets the Intent action to ACTION_SEND, defines an Intent extra
to hold the text, and sets the type of the data to the MIME type "text/plain".

Intent intent = new Intent();


intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, textMessage);
intent.setType("text/plain");

Pictorial representation of Implicit intents


Each intent filter specifies the type of intents it accepts based on the intent's action,
data, and category.

Activity A creates an intent with the required action and sends it to an android system
using the startActivity() method. The android system will search for an intent filter that matches
the intent in all apps. Whenever the match found the system starts matching activity (Activity B)
by invoking the onCreate() method.

In android when we create implicit intents, the android system will search for matching components
by comparing the contents of intent with intent filters which defined in the manifest file of other
apps on the device. If the matching component found, the system starts that component and sends
it to the Intent object. In case, if multiple intent filters are matched then the system displays a dialog
so that the user can pick which app to use.

In android, an Intent Filter is an expression in the app’s manifest file and it is used to
specify the type of intents that the component would like to receive. In case if we create an Intent
Filter for activity, there is a possibility for other apps to start our activity by sending a certain type
of intent otherwise the activity can be started only by an explicit intent.

For example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(“https://www.geeksforgeeks.org”));
startActivity(intent);

Intent filters
Define Intent filters with one or more <intent-filter> elements in
the AndroidManifest.xml file.Inside <intent-filter>, specify the type of intent your
activity can handle. The Android system matches an implicit intent with an activity
or other app component only if the fields in the Intent object match the Intent filters
for that component.
An Intent filter may contain the following elements, which correspond to the fields
in the Intent object described above:

• <action>: The Intent action that the activity accepts.


• <data>: The type of data accepted, including the MIMEtype or other attributes
of the data URI (such as scheme, host, port, and path).
• <category>: The Intent category.

For example, the main Activity for your app includes this <intent-filter> element,
which you saw in an earlier chapter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This Intent filter has the action MAIN and the category LAUNCHER.
The <action> element specifies that this is the app's "main" entry point.
The <category> element specifies that this activity should be listed in the system's
app launcher (to allow users to launch the activity). Only the main activity for your
app should have this Intent filter.
Actions
An Intent filter can declare zero or more <action> elements for the Intent action.
The action is defined in the name attribute, and consists of the
string "android.intent.action." plus the name of the Intent action, minus
the ACTION_ prefix. So, for example, an implicit Intent with the
action ACTION_VIEW matches an Intent filter whose action
is android.intent.action.VIEW.
For example, this Intent filter matches either ACTION_EDIT and ACTION_VIEW:
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
To get through this filter, the action specified in the incoming Intent object must
match at least one of the actions. You must include at least one Intent action for an
incoming implicit Intent to match.

Categories
An Intent filter can declare zero or more <category> elements for Intent categories.
The category is defined in the name attribute, and consists of the
string "android.intent.category." plus the name of the Intent category, minus
the CATEGORY prefix.
For example, this Intent filter matches
either CATEGORY_DEFAULT and CATEGORY_BROWSABLE:
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

Data
An Intent filter can declare zero or more <data> elements for the URI contained in
the Intent data. As the Intent data consists of a URI and (optionally) a MIME type,
you can create an Intent filter for various aspects of that data, including:

• URI Scheme
• URI Host
• URI Path
• Mime type

For example, this Intent filter matches any data Intent with a URI scheme
of http and a MIME type of either "video/mpeg" or "audio/mpeg".
<intent-filter>
<data android:mimeType="video/mpeg" android:scheme="http" />
<data android:mimeType="audio/mpeg" android:scheme="http" />
</intent-filter>
Note: A Uri object is usually used to tell a ContentProvider what we want to access by
reference. It is an immutable one-to-one mapping to a resource or data. The method Uri.
parse creates a new Uri object from a properly formated String .

URI(Uniform resource identifier) as its name suggests is used to identify resource(whether


it be a page of text, a video or sound clip, a still or animated image, or a program).

Android uses URI string as the basis for requesting data in a content provider (i.e. to
retrieve a list of contacts) and for requesting actions (i.e. opening a webpage in a browser)

Explicit Intent:
Explicit intent specifies the component in an application, that is which class to be invoked.
You can pass the information from one activity to another using explicit intent.

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);


startActivity(i);

You might also like