Android Tutorials for Beginners — Intents and Filters

In the series for this weeks discussion the topic chosen was Intent and Intent Filter are as follows,

  • What is an Intent?
  • Why Intent ?
  • Intent Objects — Action, Data & Category
  • Types of Intent
  • Intent Resoultion
Recommended Reading : Best Coding Practices for Android Development

What is an Intent?

Android Intent contain an action carrying some information.Intents allow you to interact with components that belong either at the same application or outside that application

Intent can be used to:

  • To start an activity
  • To start a service
  • To deliver a broadcast

For Example, Suppose you are on the news feed screen (which is one Activity), and want to view a pic posted by our friend. When you click on the photo, the intent associated with the click event of photo is fired which communicates the message, and the Photo page opens (which is a new Activity).

Why Intent ?

Intent is used to communicate,share data between components.

Intent contains the following things,

  • Component name
  • Action
  • Data
  • Category
  • Extras
  • Flags

Here, the detailed explanation about Intent objects

  • Component name — This information is optional. In case the component name is set, the Android system directly maps the intent to the target component. Else, the system utilizes other information to locate the suitable target. The component name is set by setComponent (), setClass () and is read by getComponent ().
  • Action — This is mandatory part of information names the action an Android component should take on receiving the Intent , or is being notified to the system and the action that has already taken place. The Android system defines a number of Action Constants

Here, We are listed two

ACTION_BATTERY_LOW — Battery low notification
ACTION_CALL — Initiate a phone call
  • Data: Contains the Uniform Resource Identifier (URI) of the data. There are certain types of data specifications for different actions. For instance, if the action defined were ACTION_CALL, the data field would contain a number to call (tel: URI). For correctly matching the Intent to a suitable component and defining the data type (in addition to the URI) helps. And, a component to display image should be called to display image only, not to play an audio file.
  • Category: This category is optional part and it defines the category of the component that would handle by the Intent.
  • Extras: This is the key value pair for additional information that needs delivered to the component handling the intent.
  • Flags : Flags are defined in the Intent class that function as metadata for the intent and The flays may instruct the Android system to launch an activity in a specific manner.
Download Link : Complete Android Topics to be a Professional

Types of Intent

  • Explicit Intents
  • Implicit Intents

Explicit Intents — An explicit intent is an Intent where you explicitly define the component that needs to be called by the Android System. Normally,Explicit intents are used to start components within your own application.

To Create Explicit Intent

Intent intent = new Intent(get ApplicationContext(),Second Activity.class);

Implicit Intents — The implicit intent is the intent where instead of defining the exact components, you define the action that you want to perform for different activities.

To Create Implicit Intent

Intent i=new Intent();

Here is What Developers Guide tells about Intent and Intent Filter

Intent Resoultion

Because of the arbitrary nature of Implicit Intents, the system uses a process called Intent Resolution to map them correctly. The system does by intent matching with the description have the default descriptions in the system.

Intent Filters are associated with the different Android components, and able to receive Intent. Intent filter is a way for Android components to declare their capabilities to the Android system.

Filters play an main role in defining the kind of intent an Android component can receive. A component have no filters can receive Explicit Intents only, whereas components with filters can capable of receiving both Implicit and Explicit Intents. The Intent Resolution uses the following information to map the Intent to the appropriate Android component:

  • The action
  • The type (data type and URI)
  • The category

Extras and flags have no roles here.

You probably now understand the concepts of Android Intent a little better.