Intent and Intent Filters

What is Intent :

An intent is represented by the android.content.Intent class. It is in the content package because intents can be used to quickly access content providers. But its use is much broader than that; in fact, the Android Developer Reference says, “An intent is an abstract description of an operation to be performed,” so intents can be used to quickly accomplish many tasks that would otherwise take more programming code. An intent is sort of a programming shortcut that’s built into the Android OS and programming environment.


An Intent object is basically a passive data object (a bundle of instructions, if you will) that both provides a description of some sort of standard operating system or developer created “action” that needs to be performed and passes the data which that action needs to operate on to the code receiving the intent.
In addition to a specified action, the Intent object also can contain relevant data needed to complete that action, as well as data type specifications, constants, flags, and even extra data related to the data needed by the action.


Because intents provide a detailed data and process communication structure among Android application components, they also can be rather complex data structures (objects).

There are three types of Intent objects that can be used inside the Android OS to communicate with activities, services, and broadcast receivers. In fact, there is one intent type for each of these.None of these types of Intent objects are allowed to intersect with (i.e., interfere with, collide with, or mistakenly be used with or by) any of the other types of Intent objects.

Android Intent Messaging via Intent Objects :

Intent object-based messages can contain up to seven different kinds of informational parts Component name: The name of the class that the intent and its action are targeting, specified by using the package name and the class name.

Action: A predefined type of action that is to be performed, such as ACTION DIAL to initiate a phone dialing sequence or ACTION VIEW to view records in a database.

Data: The actual data to be acted on, such as the address of the database records to view, or the phone number to dial.

Category: Android has predefined intents that are part of the OS that are divided into various types or categories for easy access and use. The category name tells what area of the OS the action that follows it is going to affect. For instance, CATEGORY HOME deals with the Android Home screen. An ACTION MAIN following a CATEGORY HOME would cause the Home screen to be launched in the smartphone, tablet, e-reader, or iTV.

Type: This attribute specifies the type of the data using a MIME format. It’s often left out, as Android is usually able to infer the data type from analyzing the data itself.

Flags: This allows on/off flags to be sent with the intent. Flags are not used for typical intents, but  allow more complicated intents to be crafted if needed by advanced developers.

Extras: This parameter allows any extra information that is not covered in the previous fields to be included in the intent. This allows very complex intents to be created.

With these seven different types of information, the messaging construct that an Intent object communicates can become quite an intricate data structure, if you need it to be, it can also be quite simple, depending on the application use that is involved.

The first thing an Intent object usually specifies is the name of the application component you are targeting (usually a class you create); this is specified via the package and class name, like so:

ComponentName(string package, string class)

The component name is optional. If it is not specified, the Android OS will utilize all of the other information contained within the Intent object to infer what component of the application or android OS the Intent object should be passed to for further processing. 

It is safer to always specify this information. On the other hand, intents are intended to be used as programming shortcuts, and for many standard or common instances, Android is designed to properly infer how to process them. It is important to note that in many cases the action constant that is specified determines the type and structure of the data of the Intent object. 

The data parameter is as important to the overall result of the intent resolution as the specified action to be performed. Without providing the data for the action to operate on, the action is as useless as the data would be without any action to be performed on it!

The ACTION DIAL action constant is a good example; it targets an activity and displays the smartphone dialing utility with the phone number (the data passed to it) to be dialed. The data is the phone number the user entered into the user interface, and because the action constant is ACTION DIAL, Android can infer that the data passed to it is the phone number to be dialed.

A closely related part of the Intent object specification is the data’s MIME type, which explicitly tells Android what type of data the intent should be working with so that, for example, audio data doesn’t encounter an image processing routine.

The type part of the Intent object allows you to specify an explicit MIME data definition or data type that, if present, overrides any inference of the data type by the Android OS. You may already be familiar with the MIME data type declarations, as they are quite common on web servers and other types of data servers.


MIME stands for “Multipurpose Internet Mail Extensions” and was originally designed for e-mail  servers to define their support for different types of data. It has since been extended to other server definitions of supported data and content types, and to communication protocols (such as HTTP) data type definitions, and now to Android OS to define content data types as well. Suffice it to say that MIME has become a standard for defining content data types in a myriad of computing environments. Examples of MIME definition include the following:

  • Content-Type: text/plain
  • Content-Type: image/jpeg
  • Content-Type: audio/mp3
  • Content-Type: video/mp4
  • Content-Type: application/msword

Another important parameter of an Intent object is the category, which is meant to give additional or more fine-tuned information about the action that is specified to execute. This is more useful with some actions than with others.


Implicit Intents and Explicit Intents :

Explicit Intents :

Explicit intents use the component portion of the Intent object via the ComponentName data field. You’ll generally use these when working with applications you have developed, as you’ll know which packages and classes are appropriate for the Intent object to send an action message and data to be acted on. 

Because the component is specified explicitly, this type of intent is also safer, as there is zero room for error in interpretation. Best programming practices dictate that you thoroughly document your code and thereby give other programmers using your intent code the proper component name information. However in the real world, this best case does not always happen, and thus Android also has implicit intents and intent filters to handle other scenarios.

There are two ways to specify a component. One way is via the setComponent() method, which uses the 
ComponentName object:

.setComponent(ComponentName);

The other way is using the setClass(Context, Class) method to provide the exact class to use to process the intent. Sometimes this is the only information in the intent, especially if the desired  result from using the intent 
is simply to launch parallel activities that are internal to the application when they are needed by the user.


Implicit Intents : 

Implicit intents are those that don’t specify the component within the intent object. This means that Android has to infer from the other parameters in the intent object what code it needs to pass the intent message to for successful processing. Android does this inference based on a comparison of  the various actions, data, and categories defined in the intent object with the code components that  are available to process the intent. This is usually done via intent filters that are defined in the AndroidManifest.xml file.

Intent filters are declared in AndroidManifest.xml using the <intent-filter> tag, and they filter based on three of the seven attributes of the Intent object—action, data, and category. Intent filters provide a description of intent object structures that need to be matched as well as a priority attribute to be used if more than one match is encountered. If no action filters are specified, the action parameter of the intent will not be tested at all, moving the testing on to the data parameter of the intent. 

If no data filters are specified, then only intents that contain no data will be matched. Here is an example intent-filter definition from an AndroidManifest.xml file that specifies that video MPEG4 and audio MPEG3 can be retrieved from the internet via HTTP:

<intent-filter>
<data android:mimeType="video/mp4" android:scheme="http" />
<data android:mimeType="audio/mp3" android:scheme="http" />

</intent-filter>

No comments:

Post a Comment